package controllers import ( "errors" "fmt" "log" "strings" "git.aiterp.net/AiteRP/aitestory/server" "git.aiterp.net/gisle/wrouter/auth" "github.com/sadbox/mediawiki" ) // WikiAthenticator talks with the wiki, allowing users // to log in type WikiAthenticator struct{} // ID is for a potential multi-login future func (wikiAuth *WikiAthenticator) ID() string { return "wiki" } // Name is for a potential multi-login future func (wikiAuth *WikiAthenticator) Name() string { return "Wiki" } // Find finds a user that has logged in at least once func (wikiAuth *WikiAthenticator) Find(username string) *auth.User { db := server.Main.DB rows, err := db.Query("SELECT id,role FROM `user` WHERE id=?", username) if err != nil { log.Println("WikiAthenticator.Find:", err) return nil } defer rows.Close() if !rows.Next() { return nil } user := auth.NewUser(wikiAuth, "", "member", make(map[string]string, 4)) role := "member" rows.Scan(&user.ID, &role) user.Data["role"] = role return user } // Login login func (wikiAuth *WikiAthenticator) Login(username, password string) (*auth.User, error) { db := server.Main.DB // Connect to the wiki client, err := mediawiki.New(server.Main.Config.Wiki.URL, server.UserAgent) if err != nil { log.Fatal(err) } // Log into the wiki with the credementials err = client.Login(username, password) if err != nil { return nil, fmt.Errorf("Login failed %v", err) } // Look up the user rows, err := db.Query("SELECT id,role FROM `user` WHERE id=?", username) if err != nil { return nil, fmt.Errorf("Login failed %v", err) } // If none was found, just create a new record with the role of member if !rows.Next() { _, err = db.Exec("INSERT INTO `user` (id, role) VALUES (?, 'member')", username) if err != nil { return nil, fmt.Errorf("Login failed %v", err) } return auth.NewUser(wikiAuth, username, "member", nil), nil } // If the user was found, read it in userid, role := "", "" err = rows.Scan(&userid, &role) if err != nil { return nil, fmt.Errorf("Login failed %v", err) } userid = strings.Split(userid, "@")[0] // Make the user return auth.NewUser(wikiAuth, userid, "member", nil), nil } // Register just tells the user that they can't. func (wikiAuth *WikiAthenticator) Register(username, password string, data map[string]string) (*auth.User, error) { return nil, errors.New("Registration not allowed") }