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.
96 lines
2.2 KiB
96 lines
2.2 KiB
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{}
|
|
|
|
func (wikiAuth *WikiAthenticator) ID() string {
|
|
return "wiki"
|
|
}
|
|
|
|
func (wikiAuth *WikiAthenticator) Name() string {
|
|
return "Wiki"
|
|
}
|
|
|
|
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", nil)
|
|
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=?", client.BasicAuthUser)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Login failed %v", err)
|
|
return nil, nil
|
|
}
|
|
|
|
// 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')", client.BasicAuthUser)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Login failed %v", err)
|
|
}
|
|
|
|
return auth.NewUser(wikiAuth, client.BasicAuthUser, "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
|
|
}
|
|
|
|
func (wikiAuth *WikiAthenticator) Register(username, password string, data map[string]string) (*auth.User, error) {
|
|
return nil, errors.New("Registration not allowed")
|
|
}
|