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.

102 lines
2.5 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
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. fullID := wikiAuth.ID() + ":" + username
  26. rows, err := db.Query("SELECT id,role FROM `user` WHERE id=?", fullID)
  27. if err != nil {
  28. log.Println("WikiAthenticator.Find:", err)
  29. return nil
  30. }
  31. defer rows.Close()
  32. if !rows.Next() {
  33. return nil
  34. }
  35. user := auth.NewUser(wikiAuth, "", "member", make(map[string]string, 4))
  36. role := "member"
  37. rows.Scan(&user.ID, &role)
  38. user.Data["role"] = role
  39. user.ID = strings.SplitN(user.ID, ":", 2)[1]
  40. return user
  41. }
  42. // Login login
  43. func (wikiAuth *WikiAthenticator) Login(username, password string) (*auth.User, error) {
  44. db := server.Main.DB
  45. fullID := wikiAuth.ID() + ":" + username
  46. // Connect to the wiki
  47. client, err := mediawiki.New(server.Main.Config.Wiki.URL, server.UserAgent)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. // Log into the wiki with the credementials
  52. err = client.Login(username, password)
  53. if err != nil {
  54. return nil, fmt.Errorf("Login failed %v", err)
  55. }
  56. // Look up the user
  57. rows, err := db.Query("SELECT id,role FROM `user` WHERE id=?", fullID)
  58. if err != nil {
  59. return nil, fmt.Errorf("Login failed %v", err)
  60. }
  61. // If none was found, just create a new record with the role of member
  62. if !rows.Next() {
  63. _, err = db.Exec("INSERT INTO `user` (id, role) VALUES (?, 'member')", fullID)
  64. if err != nil {
  65. return nil, fmt.Errorf("Login failed %v", err)
  66. }
  67. return auth.NewUser(wikiAuth, username, "member", nil), nil
  68. }
  69. // If the user was found, read it in
  70. userid, role := "", ""
  71. err = rows.Scan(&userid, &role)
  72. if err != nil {
  73. return nil, fmt.Errorf("Login failed %v", err)
  74. }
  75. userid = strings.Split(userid, "@")[0]
  76. // Make the user
  77. return auth.NewUser(wikiAuth, strings.Split(userid, ":")[1], "member", nil), nil
  78. }
  79. // Register just tells the user that they can't.
  80. func (wikiAuth *WikiAthenticator) Register(username, password string, data map[string]string) (*auth.User, error) {
  81. return nil, errors.New("Registration not allowed")
  82. }