Skip to content

Commit 770bbd9

Browse files
committed
Add refresh token example
1 parent 24b9ab5 commit 770bbd9

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

example/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,17 @@ Open the browser [http://localhost:9094/try](http://localhost:9094/try)
4444
"expires_in": 7195,
4545
"user_id": "000000"
4646
}
47+
```
48+
49+
## Refresh token
50+
51+
Open the browser [http://localhost:9094/refresh](http://localhost:9094/refresh)
52+
53+
```
54+
{
55+
"access_token": "0IIL4_AJN2-SR0JEYZVQWG",
56+
"token_type": "Bearer",
57+
"refresh_token": "AG6-63MLXUEFUV2Q_BLYIW",
58+
"expiry": "2019-01-09T23:03:16.374062+08:00"
59+
}
4760
```

example/client/client.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"log"
99
"net/http"
10+
"time"
1011

1112
"golang.org/x/oauth2"
1213
)
@@ -59,6 +60,25 @@ func main() {
5960
e.Encode(token)
6061
})
6162

63+
http.HandleFunc("/refresh", func(w http.ResponseWriter, r *http.Request) {
64+
if globalToken == nil {
65+
http.Redirect(w, r, "/", http.StatusFound)
66+
return
67+
}
68+
69+
globalToken.Expiry = time.Now()
70+
token, err := config.TokenSource(context.Background(), globalToken).Token()
71+
if err != nil {
72+
http.Error(w, err.Error(), http.StatusInternalServerError)
73+
return
74+
}
75+
76+
globalToken = token
77+
e := json.NewEncoder(w)
78+
e.SetIndent("", " ")
79+
e.Encode(token)
80+
})
81+
6282
http.HandleFunc("/try", func(w http.ResponseWriter, r *http.Request) {
6383
if globalToken == nil {
6484
http.Redirect(w, r, "/", http.StatusFound)

manage/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ var (
3535
DefaultImplicitTokenCfg = &Config{AccessTokenExp: time.Hour * 1}
3636
DefaultPasswordTokenCfg = &Config{AccessTokenExp: time.Hour * 2, RefreshTokenExp: time.Hour * 24 * 7, IsGenerateRefresh: true}
3737
DefaultClientTokenCfg = &Config{AccessTokenExp: time.Hour * 2}
38-
DefaultRefreshTokenCfg = &RefreshingConfig{IsRemoveAccess: true, IsRemoveRefreshing: true}
38+
DefaultRefreshTokenCfg = &RefreshingConfig{IsGenerateRefresh: true, IsRemoveAccess: true, IsRemoveRefreshing: true}
3939
)

0 commit comments

Comments
 (0)