commit fd0550afbf94c865257c1fb45709dc8766324938 Author: Gisle Aune Date: Sat Jan 13 10:38:09 2018 +0100 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1db2eef --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +credementials.json \ No newline at end of file diff --git a/wikiauth.go b/wikiauth.go new file mode 100644 index 0000000..01b1e0d --- /dev/null +++ b/wikiauth.go @@ -0,0 +1,34 @@ +package wikiauth + +import ( + "github.com/sadbox/mediawiki" +) + +// WikiAuth is the structure responsible for wiki authentication. It's immuitable +// and thread safe. The only time it connects is to log in. +type WikiAuth struct { + url string +} + +// New creates a new wiki authenticator instance. +func New(url string) *WikiAuth { + return &WikiAuth{url} +} + +// Login connects to the wiki and logs in. It will not reuse clients between +// logins, so it's thus threadsafe. +func (wikiAuth *WikiAuth) Login(username, password string) error { + client, err := mediawiki.New(wikiAuth.url, "") + if err != nil { + return err + } + + err = client.Login(username, password) + if err != nil { + return err + } + + client.Logout() + + return nil +} diff --git a/wikiauth_test.go b/wikiauth_test.go new file mode 100644 index 0000000..12dcfa7 --- /dev/null +++ b/wikiauth_test.go @@ -0,0 +1,58 @@ +package wikiauth_test + +import ( + "encoding/json" + "os" + "testing" + + "git.aiterp.net/AiteRP/wikiauth" +) + +func TestAuth(t *testing.T) { + auth := struct { + Username string + Password string + }{} + + file, err := os.Open("./credementials.json") + if err != nil { + t.Skip(err) + return + } + err = json.NewDecoder(file).Decode(&auth) + if err != nil { + t.Skip(err) + return + } + + wiki := wikiauth.New("https://wiki.aiterp.net/api.php") + err = wiki.Login(auth.Username, auth.Password) + if err != nil { + t.Error(err) + } + + err = wiki.Login(auth.Username+"_wrong", auth.Password) + if err == nil { + t.Error("Wrong username accepted!") + } + + err = wiki.Login(auth.Username, auth.Password+"_wrong") + if err == nil { + t.Error("Wrong password accepted!") + } + + err = wiki.Login(auth.Username, "") + if err == nil { + t.Error("No password accepted!") + } + + err = wiki.Login("", auth.Password) + if err == nil { + t.Error("No username accepted!") + } + + err = wiki.Login("", "") + if err == nil { + t.Error("No credementials accepted!") + } +}