Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dex
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hdacloud
dex
Commits
ed20fee2
Commit
ed20fee2
authored
8 years ago
by
Eric Chiang
Browse files
Options
Downloads
Patches
Plain Diff
cmd/example-app: fix refreshing
parent
f4bbab50
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
cmd/example-app/main.go
+25
-17
25 additions, 17 deletions
cmd/example-app/main.go
cmd/example-app/templates.go
+7
-2
7 additions, 2 deletions
cmd/example-app/templates.go
with
32 additions
and
19 deletions
cmd/example-app/main.go
+
25
−
17
View file @
ed20fee2
...
...
@@ -241,7 +241,7 @@ func (a *app) handleLogin(w http.ResponseWriter, r *http.Request) {
authCodeURL
:=
""
scopes
=
append
(
scopes
,
"openid"
,
"profile"
,
"email"
)
if
r
.
FormValue
(
"offline_ac
e
css"
)
!=
"yes"
{
if
r
.
FormValue
(
"offline_acc
e
ss"
)
!=
"yes"
{
authCodeURL
=
a
.
oauth2Config
(
scopes
)
.
AuthCodeURL
(
exampleAppState
)
}
else
if
a
.
offlineAsScope
{
scopes
=
append
(
scopes
,
"offline_access"
)
...
...
@@ -254,34 +254,42 @@ func (a *app) handleLogin(w http.ResponseWriter, r *http.Request) {
}
func
(
a
*
app
)
handleCallback
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
errMsg
:=
r
.
FormValue
(
"error"
);
errMsg
!=
""
{
http
.
Error
(
w
,
errMsg
+
": "
+
r
.
FormValue
(
"error_description"
),
http
.
StatusBadRequest
)
return
}
if
state
:=
r
.
FormValue
(
"state"
);
state
!=
exampleAppState
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"expected state %q got %q"
,
exampleAppState
,
state
),
http
.
StatusBadRequest
)
return
}
code
:=
r
.
FormValue
(
"code"
)
refresh
:=
r
.
FormValue
(
"refresh_token"
)
var
(
err
error
token
*
oauth2
.
Token
)
oauth2Config
:=
a
.
oauth2Config
(
nil
)
switch
{
case
code
!=
""
:
switch
r
.
Method
{
case
"GET"
:
// Authorization redirect callback from OAuth2 auth flow.
if
errMsg
:=
r
.
FormValue
(
"error"
);
errMsg
!=
""
{
http
.
Error
(
w
,
errMsg
+
": "
+
r
.
FormValue
(
"error_description"
),
http
.
StatusBadRequest
)
return
}
code
:=
r
.
FormValue
(
"code"
)
if
code
==
""
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"no code in request: %q"
,
r
.
Form
),
http
.
StatusBadRequest
)
return
}
if
state
:=
r
.
FormValue
(
"state"
);
state
!=
exampleAppState
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"expected state %q got %q"
,
exampleAppState
,
state
),
http
.
StatusBadRequest
)
return
}
token
,
err
=
oauth2Config
.
Exchange
(
a
.
ctx
,
code
)
case
refresh
!=
""
:
case
"POST"
:
// Form request from frontend to refresh a token.
refresh
:=
r
.
FormValue
(
"refresh_token"
)
if
refresh
==
""
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"no refresh_token in request: %q"
,
r
.
Form
),
http
.
StatusBadRequest
)
return
}
t
:=
&
oauth2
.
Token
{
RefreshToken
:
refresh
,
Expiry
:
time
.
Now
()
.
Add
(
-
time
.
Hour
),
}
token
,
err
=
oauth2Config
.
TokenSource
(
r
.
Context
(),
t
)
.
Token
()
default
:
http
.
Error
(
w
,
fmt
.
Sprintf
(
"
no code in request
: %
q
"
,
r
.
Form
),
http
.
StatusBadRequest
)
http
.
Error
(
w
,
fmt
.
Sprintf
(
"
method not implemented
: %
s
"
,
r
.
Method
),
http
.
StatusBadRequest
)
return
}
...
...
This diff is collapsed.
Click to expand it.
cmd/example-app/templates.go
+
7
−
2
View file @
ed20fee2
...
...
@@ -8,7 +8,7 @@ import (
var
indexTmpl
=
template
.
Must
(
template
.
New
(
"index.html"
)
.
Parse
(
`<html>
<body>
<form action="/login">
<form action="/login"
method="post"
>
<p>
Authenticate for:<input type="text" name="cross_client" placeholder="list of client-ids">
</p>
...
...
@@ -50,8 +50,13 @@ pre {
<body>
<p> Token: <pre><code>{{ .IDToken }}</code></pre></p>
<p> Claims: <pre><code>{{ .Claims }}</code></pre></p>
{{ if .RefreshToken }}
<p> Refresh Token: <pre><code>{{ .RefreshToken }}</code></pre></p>
<p><a href="{{ .RedirectURL }}?refresh_token={{ .RefreshToken }}">Redeem refresh token</a><p>
<form action="{{ .RedirectURL }}" method="post">
<input type="hidden" name="refresh_token" value="{{ .RefreshToken }}">
<input type="submit" value="Redeem refresh token">
</form>
{{ end }}
</body>
</html>
`
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment