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.

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