Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
SnailLife Go
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
Liza
SnailLife Go
Commits
9fd17c89
Commit
9fd17c89
authored
Jul 28, 2017
by
Liza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create json util; add logout test
parent
ecb83ff7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
71 additions
and
18 deletions
+71
-18
auth0.go
lib/infrastructure/auth/cli/auth0/auth0.go
+31
-14
auth0_test.go
lib/infrastructure/auth/cli/auth0/tests/auth0_test.go
+11
-4
http.go
lib/infrastructure/http/http.go
+10
-0
util.go
lib/infrastructure/util/util.go
+19
-0
No files found.
lib/infrastructure/auth/cli/auth0/auth0.go
View file @
9fd17c89
...
...
@@ -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
:=
http
2
.
PostAndGetResBody
(
url
,
postSpec
)
body
,
err
:=
http
util
.
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
:=
http
2
.
PostAndGetResBody
(
url
,
postSpec
)
body
,
err
:=
http
util
.
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
=
t
oken
.
(
string
)
if
aToken
!=
nil
{
ap
.
accessToken
=
aT
oken
.
(
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
...
...
lib/infrastructure/auth/cli/auth0/tests/auth0_test.go
View file @
9fd17c89
...
...
@@ -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",
...
...
lib/infrastructure/http/http.go
View file @
9fd17c89
...
...
@@ -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
{
...
...
lib/infrastructure/util/util.go
View file @
9fd17c89
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment