commit
fd0550afbf
3 changed files with 93 additions and 0 deletions
-
1.gitignore
-
34wikiauth.go
-
58wikiauth_test.go
@ -0,0 +1 @@ |
|||
credementials.json |
|||
@ -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 |
|||
} |
|||
@ -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!") |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue