Skip to content

Commit cc5f83e

Browse files
committed
Add example with /try
1 parent c189294 commit cc5f83e

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

example/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,26 @@ $ ./client
2222

2323
## Open the browser
2424

25-
[http://localhost:9094](http://localhost:9094)
25+
[http://localhost:9094](http://localhost:9094)
26+
27+
```
28+
{
29+
"access_token": "GIGXO8XWPQSAUGOYQGTV8Q",
30+
"token_type": "Bearer",
31+
"refresh_token": "5FBLXQ47XJ2MGTY8YRZQ8W",
32+
"expiry": "2019-01-08T01:53:45.868194+08:00"
33+
}
34+
```
35+
36+
37+
## Try access token
38+
39+
Open the browser [http://localhost:9094/try](http://localhost:9094/try)
40+
41+
```
42+
{
43+
"client_id": "222222",
44+
"expires_in": 7195,
45+
"user_id": "000000"
46+
}
47+
```

example/client/client.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,30 @@ package main
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
7+
"io"
68
"log"
79
"net/http"
810

911
"golang.org/x/oauth2"
1012
)
1113

14+
const (
15+
authServerURL = "http://localhost:9096"
16+
)
17+
1218
var (
1319
config = oauth2.Config{
1420
ClientID: "222222",
1521
ClientSecret: "22222222",
1622
Scopes: []string{"all"},
1723
RedirectURL: "http://localhost:9094/oauth2",
1824
Endpoint: oauth2.Endpoint{
19-
AuthURL: "http://localhost:9096/authorize",
20-
TokenURL: "http://localhost:9096/token",
25+
AuthURL: authServerURL + "/authorize",
26+
TokenURL: authServerURL + "/token",
2127
},
2228
}
29+
globalToken *oauth2.Token // Non-concurrent security
2330
)
2431

2532
func main() {
@@ -45,9 +52,27 @@ func main() {
4552
http.Error(w, err.Error(), http.StatusInternalServerError)
4653
return
4754
}
55+
globalToken = token
56+
4857
e := json.NewEncoder(w)
4958
e.SetIndent("", " ")
50-
e.Encode(*token)
59+
e.Encode(token)
60+
})
61+
62+
http.HandleFunc("/try", func(w http.ResponseWriter, r *http.Request) {
63+
if globalToken == nil {
64+
http.Redirect(w, r, "/", http.StatusFound)
65+
return
66+
}
67+
68+
resp, err := http.Get(fmt.Sprintf("%s/test?access_token=%s", authServerURL, globalToken.AccessToken))
69+
if err != nil {
70+
http.Error(w, err.Error(), http.StatusBadRequest)
71+
return
72+
}
73+
defer resp.Body.Close()
74+
75+
io.Copy(w, resp.Body)
5176
})
5277

5378
log.Println("Client is running at 9094 port.")

example/server/server.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"log"
56
"net/http"
67
"net/url"
78
"os"
9+
"time"
810

911
"github.com/go-session/session"
1012
"gopkg.in/oauth2.v3/errors"
@@ -16,6 +18,8 @@ import (
1618

1719
func main() {
1820
manager := manage.NewDefaultManager()
21+
manager.SetAuthorizeCodeTokenCfg(manage.DefaultAuthorizeCodeTokenCfg)
22+
1923
// token store
2024
manager.MustTokenStorage(store.NewMemoryTokenStore())
2125

@@ -71,6 +75,23 @@ func main() {
7175
}
7276
})
7377

78+
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
79+
token, err := srv.ValidationBearerToken(r)
80+
if err != nil {
81+
http.Error(w, err.Error(), http.StatusBadRequest)
82+
return
83+
}
84+
85+
data := map[string]interface{}{
86+
"expires_in": int64(token.GetAccessCreateAt().Add(token.GetAccessExpiresIn()).Sub(time.Now()).Seconds()),
87+
"client_id": token.GetClientID(),
88+
"user_id": token.GetUserID(),
89+
}
90+
e := json.NewEncoder(w)
91+
e.SetIndent("", " ")
92+
e.Encode(data)
93+
})
94+
7495
log.Println("Server is running at 9096 port.")
7596
log.Fatal(http.ListenAndServe(":9096", nil))
7697
}

0 commit comments

Comments
 (0)