The backend for the AiteStory website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package controllers
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "strings"
  7. "git.aiterp.net/AiteRP/aitestory/server"
  8. "git.aiterp.net/gisle/wrouter/auth"
  9. "github.com/sadbox/mediawiki"
  10. )
  11. // WikiAthenticator talks with the wiki, allowing users
  12. // to log in
  13. type WikiAthenticator struct{}
  14. // ID is for a potential multi-login future
  15. func (wikiAuth *WikiAthenticator) ID() string {
  16. return "wiki"
  17. }
  18. // Name is for a potential multi-login future
  19. func (wikiAuth *WikiAthenticator) Name() string {
  20. return "Wiki"
  21. }
  22. // Find finds a user that has logged in at least once
  23. func (wikiAuth *WikiAthenticator) Find(username string) *auth.User {
  24. db := server.Main.DB
  25. rows, err := db.Query("SELECT id,role FROM `user` WHERE id=?", username)
  26. if err != nil {
  27. log.Println("WikiAthenticator.Find:", err)
  28. return nil
  29. }
  30. defer rows.Close()
  31. if !rows.Next() {
  32. return nil
  33. }
  34. user := auth.NewUser(wikiAuth, "", "member", make(map[string]string, 4))
  35. role := "member"
  36. rows.Scan(&user.ID, &role)
  37. user.Data["role"] = role
  38. return user
  39. }
  40. // Login login
  41. func (wikiAuth *WikiAthenticator) Login(username, password string) (*auth.User, error) {
  42. db := server.Main.DB
  43. // Connect to the wiki
  44. client, err := mediawiki.New(server.Main.Config.Wiki.URL, server.UserAgent)
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. // Log into the wiki with the credementials
  49. err = client.Login(username, password)
  50. if err != nil {
  51. return nil, fmt.Errorf("Login failed %v", err)
  52. }
  53. // Look up the user
  54. rows, err := db.Query("SELECT id,role FROM `user` WHERE id=?", username)
  55. if err != nil {
  56. return nil, fmt.Errorf("Login failed %v", err)
  57. }
  58. // If none was found, just create a new record with the role of member
  59. if !rows.Next() {
  60. _, err = db.Exec("INSERT INTO `user` (id, role) VALUES (?, 'member')", username)
  61. if err != nil {
  62. return nil, fmt.Errorf("Login failed %v", err)
  63. }
  64. return auth.NewUser(wikiAuth, username, "member", nil), nil
  65. }
  66. // If the user was found, read it in
  67. userid, role := "", ""
  68. err = rows.Scan(&userid, &role)
  69. if err != nil {
  70. return nil, fmt.Errorf("Login failed %v", err)
  71. }
  72. userid = strings.Split(userid, "@")[0]
  73. // Make the user
  74. return auth.NewUser(wikiAuth, userid, "member", nil), nil
  75. }
  76. // Register just tells the user that they can't.
  77. func (wikiAuth *WikiAthenticator) Register(username, password string, data map[string]string) (*auth.User, error) {
  78. return nil, errors.New("Registration not allowed")
  79. }