Commit 9fd17c89 by Liza

Create json util; add logout test

parent ecb83ff7
......@@ -2,9 +2,9 @@ package auth0
import (
"fmt"
"encoding/json"
"errors"
http2 "gitlab.com/drakonka/gosnaillife/lib/infrastructure/http"
httputil "gitlab.com/drakonka/gosnaillife/lib/infrastructure/http"
"gitlab.com/drakonka/gosnaillife/lib/infrastructure/util"
)
......@@ -29,16 +29,18 @@ func (ap *Auth0) Register(email, password string) (id string, err error) {
"password":"%s",
"connection":"Username-Password-Authentication"}`, ap.clientId, email, password)
body, err := http2.PostAndGetResBody(url, postSpec)
body, err := httputil.PostAndGetResBody(url, postSpec)
if err != nil {
return id, err
}
var data map[string]interface{}
err = json.Unmarshal([]byte(string(body)), &data)
m, err := util.FindInJson([]byte(string(body)), []string{"_id"})
if err != nil {
return id, err
}
if val, ok := data["_id"]; ok {
id = "auth0|" + val.(string)
if m["_id"] != nil {
id = "auth0|" + m["_id"].(string)
}
return id, err
}
......@@ -51,28 +53,43 @@ func (ap *Auth0) Login(username, password string) (err error) {
"client_secret": "%s",
"client_id":"%s"}`, username, password, ap.clientSecret, ap.clientId)
body, err := http2.PostAndGetResBody(url, postSpec)
body, err := httputil.PostAndGetResBody(url, postSpec)
if err != nil {
return err
}
var data map[string]interface{}
err = json.Unmarshal([]byte(string(body)), &data)
m, err := util.FindInJson([]byte(string(body)), []string{"error", "error_description", "access_token"})
if err != nil {
return err
}
if e, ok := data["error"]; ok {
msg := fmt.Sprintf("%s - %s", e.(string), data["error_description"].(string))
e := m["error"]
eDesc := m["error_description"]
aToken := m["access_token"]
if e != nil {
msg := fmt.Sprintf("%s - %s", e.(string), eDesc.(string))
err = errors.New(msg)
return err
}
if token, ok := data["access_token"]; ok {
ap.accessToken = token.(string)
if aToken != nil {
ap.accessToken = aToken.(string)
} else {
err = errors.New("Access token not found in response")
}
return err
}
func (ap *Auth0) Logout() (err error) {
url := ap.domain + "v2/logout"
body, err := httputil.GetAndGetResBody(url)
if err != nil {
return err
}
fmt.Println(string(body))
return err
}
func (ap *Auth0) HasAccessToken() bool {
if ap.accessToken != "" {
return true
......
......@@ -26,7 +26,7 @@ func TestMain(m *testing.M) {
func TestAuth0Signup(t *testing.T) {
fmt.Printf("Creating user with ID: %s, pass: %s", creds.email, creds.password)
fmt.Printf("Creating user with ID: %s, pass: %s\n", creds.email, creds.password)
provider := getProvider(creds)
id, err := provider.Register(creds.email, creds.password)
if err != nil {
......@@ -39,7 +39,7 @@ func TestAuth0Signup(t *testing.T) {
}
func TestAuth0Login(t *testing.T) {
fmt.Printf("Logging in with ID: %s, pass: %s", creds.email, creds.password)
fmt.Printf("Logging in with ID: %s, pass: %s\n", creds.email, creds.password)
provider := getProvider(creds)
err := provider.Login(creds.email, creds.password)
if err != nil {
......@@ -49,6 +49,15 @@ func TestAuth0Login(t *testing.T) {
}
}
func TestAuth0Logout(t *testing.T) {
fmt.Printf("Logging out user %s\n", creds.email)
provider := getProvider(creds)
err := provider.Logout()
if err != nil {
t.Errorf("Logout test failed: " + err.Error())
}
}
func getProvider(creds testCredentials) *auth0.Auth0 {
provider := auth0.Auth0{}
configCreds := map[string]string{}
......@@ -83,8 +92,6 @@ func deleteUser(token string) {
func getManagementAccessToken() (token string, err error){
url := creds.domain + "oauth/token"
audienceUrl := creds.domain + "api/v2/"
fmt.Println("AUDIENCE URL")
fmt.Println(audienceUrl)
postSpec := fmt.Sprintf( `{"client_id":"%s",
"client_secret":"%s",
"audience":"%s",
......
......@@ -24,6 +24,16 @@ func PostAndGetResBody(url, spec string) (body []byte, err error) {
return body, err
}
func GetAndGetResBody(url string) (body []byte, err error) {
res, err := http.Get(url)
if err != nil {
return body, err
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
return body, err
}
func Delete(url, authHeader string) (body []byte, err error) {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
......
......@@ -4,6 +4,7 @@ import (
"crypto/sha1"
"io"
"encoding/base32"
"encoding/json"
)
func base64Encode(str []byte) string {
......@@ -16,3 +17,21 @@ func sha256(str string) []byte {
sha := hasher.Sum(nil)
return sha
}
// Returns a map of desired elements from provided json.
func FindInJson(j []byte, wanted []string) (m map[string]interface{}, err error) {
var data map[string]interface{}
m = make(map[string]interface{})
err = json.Unmarshal(j, &data)
if len(wanted) == 0 {
return data, err
}
for _, key := range wanted {
if val, ok := data[key]; ok {
m[key] = val
} else {
m[key] = nil
}
}
return data, err
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment