Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dex
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
dex
Commits
49f446c1
Commit
49f446c1
authored
Feb 07, 2017
by
rithu leena john
Committed by
GitHub
Feb 07, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #800 from ericchiang/server-test-comments
server: clean up test comments and code flow
parents
dd415f5e
80038847
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
32 deletions
+56
-32
server_test.go
server/server_test.go
+56
-32
No files found.
server/server_test.go
View file @
49f446c1
...
...
@@ -413,6 +413,7 @@ func TestOAuth2CodeFlow(t *testing.T) {
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
// Setup a dex server.
logger
:=
&
logrus
.
Logger
{
Out
:
os
.
Stderr
,
Formatter
:
&
logrus
.
TextFormatter
{
DisableColors
:
true
},
...
...
@@ -422,7 +423,9 @@ func TestOAuth2CodeFlow(t *testing.T) {
c
.
Issuer
=
c
.
Issuer
+
"/non-root-path"
c
.
Now
=
now
c
.
IDTokensValidFor
=
idTokensValidFor
// Create a new mock callback connector for each test case.
// Testing connector that redirects without interaction with
// the user.
conn
=
mock
.
NewCallbackConnector
(
logger
)
.
(
*
mock
.
Callback
)
c
.
Connectors
=
[]
Connector
{
{
...
...
@@ -434,14 +437,17 @@ func TestOAuth2CodeFlow(t *testing.T) {
})
defer
httpServer
.
Close
()
// Query server's provider metadata.
p
,
err
:=
oidc
.
NewProvider
(
ctx
,
httpServer
.
URL
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to get provider: %v"
,
err
)
}
var
(
reqDump
,
respDump
[]
byte
// If the OAuth2 client didn't get a response, we need
// to print the requests the user saw.
gotCode
bool
reqDump
,
respDump
[]
byte
// Auth step, not token.
state
=
"a_state"
)
defer
func
()
{
...
...
@@ -450,46 +456,56 @@ func TestOAuth2CodeFlow(t *testing.T) {
}
}()
// Setup OAuth2 client.
var
oauth2Config
*
oauth2
.
Config
oauth2Server
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
URL
.
Path
==
"/callback"
{
q
:=
r
.
URL
.
Query
()
if
errType
:=
q
.
Get
(
"error"
);
errType
!=
""
{
if
desc
:=
q
.
Get
(
"error_description"
);
desc
!=
""
{
t
.
Errorf
(
"got error from server %s: %s"
,
errType
,
desc
)
}
else
{
t
.
Errorf
(
"got error from server %s"
,
errType
)
}
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
return
oauth2Client
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
URL
.
Path
!=
"/callback"
{
// User is visiting app first time. Redirect to dex.
http
.
Redirect
(
w
,
r
,
oauth2Config
.
AuthCodeURL
(
state
),
http
.
StatusSeeOther
)
return
}
// User is at '/callback' so they were just redirected _from_ dex.
q
:=
r
.
URL
.
Query
()
// Did dex return an error?
if
errType
:=
q
.
Get
(
"error"
);
errType
!=
""
{
if
desc
:=
q
.
Get
(
"error_description"
);
desc
!=
""
{
t
.
Errorf
(
"got error from server %s: %s"
,
errType
,
desc
)
}
else
{
t
.
Errorf
(
"got error from server %s"
,
errType
)
}
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
return
}
if
code
:=
q
.
Get
(
"code"
);
code
!=
""
{
gotCode
=
true
token
,
err
:=
oauth2Config
.
Exchange
(
ctx
,
code
)
if
err
!=
nil
{
t
.
Errorf
(
"failed to exchange code for token: %v"
,
err
)
return
}
err
=
tc
.
handleToken
(
ctx
,
p
,
oauth2Config
,
token
)
if
err
!=
nil
{
t
.
Errorf
(
"%s: %v"
,
tc
.
name
,
err
)
}
// Grab code, exchange for token.
if
code
:=
q
.
Get
(
"code"
);
code
!=
""
{
gotCode
=
true
token
,
err
:=
oauth2Config
.
Exchange
(
ctx
,
code
)
if
err
!=
nil
{
t
.
Errorf
(
"failed to exchange code for token: %v"
,
err
)
return
}
if
gotState
:=
q
.
Get
(
"state"
);
gotState
!=
state
{
t
.
Errorf
(
"state did not match, want=%q got=%q"
,
state
,
gotState
)
err
=
tc
.
handleToken
(
ctx
,
p
,
oauth2Config
,
token
)
if
err
!=
nil
{
t
.
Errorf
(
"%s: %v"
,
tc
.
name
,
err
)
}
w
.
WriteHeader
(
http
.
StatusOK
)
return
}
http
.
Redirect
(
w
,
r
,
oauth2Config
.
AuthCodeURL
(
state
),
http
.
StatusSeeOther
)
// Ensure state matches.
if
gotState
:=
q
.
Get
(
"state"
);
gotState
!=
state
{
t
.
Errorf
(
"state did not match, want=%q got=%q"
,
state
,
gotState
)
}
w
.
WriteHeader
(
http
.
StatusOK
)
return
}))
defer
oauth2
Server
.
Close
()
defer
oauth2
Client
.
Close
()
redirectURL
:=
oauth2Server
.
URL
+
"/callback"
// Regester the client above with dex.
redirectURL
:=
oauth2Client
.
URL
+
"/callback"
client
:=
storage
.
Client
{
ID
:
clientID
,
Secret
:
clientSecret
,
...
...
@@ -499,6 +515,7 @@ func TestOAuth2CodeFlow(t *testing.T) {
t
.
Fatalf
(
"failed to create client: %v"
,
err
)
}
// Create the OAuth2 config.
oauth2Config
=
&
oauth2
.
Config
{
ClientID
:
client
.
ID
,
ClientSecret
:
client
.
Secret
,
...
...
@@ -510,7 +527,14 @@ func TestOAuth2CodeFlow(t *testing.T) {
oauth2Config
.
Scopes
=
tc
.
scopes
}
resp
,
err
:=
http
.
Get
(
oauth2Server
.
URL
+
"/login"
)
// Login!
//
// 1. First request to client, redirects to dex.
// 2. Dex "logs in" the user, redirects to client with "code".
// 3. Client exchanges "code" for "token" (id_token, refresh_token, etc.).
// 4. Test is run with OAuth2 token response.
//
resp
,
err
:=
http
.
Get
(
oauth2Client
.
URL
+
"/login"
)
if
err
!=
nil
{
t
.
Fatalf
(
"get failed: %v"
,
err
)
}
...
...
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