From fd0550afbf94c865257c1fb45709dc8766324938 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sat, 13 Jan 2018 10:38:09 +0100 Subject: [PATCH] Initial Commit --- .gitignore | 1 + wikiauth.go | 34 ++++++++++++++++++++++++++++ wikiauth_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 wikiauth.go create mode 100644 wikiauth_test.go 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!") + } +}