Skip to content

Commit 8332330

Browse files
committed
Add use examples
1 parent fe41d7b commit 8332330

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

example/README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
# Authorization Code Grant
2-
3-
![login](https://raw.githubusercontent.com/go-oauth2/oauth2/master/example/server/static/login.png)
4-
![auth](https://raw.githubusercontent.com/go-oauth2/oauth2/master/example/server/static/auth.png)
5-
![token](https://raw.githubusercontent.com/go-oauth2/oauth2/master/example/server/static/token.png)
1+
# Use Examples
62

73
## Run Server
84

@@ -20,7 +16,9 @@ $ go build client.go
2016
$ ./client
2117
```
2218

23-
## Open the browser
19+
## Authorization Code Grant
20+
21+
### Open the browser
2422

2523
[http://localhost:9094](http://localhost:9094)
2624

@@ -34,7 +32,7 @@ $ ./client
3432
```
3533

3634

37-
## Try access token
35+
### Try access token
3836

3937
Open the browser [http://localhost:9094/try](http://localhost:9094/try)
4038

@@ -57,4 +55,33 @@ Open the browser [http://localhost:9094/refresh](http://localhost:9094/refresh)
5755
"refresh_token": "AG6-63MLXUEFUV2Q_BLYIW",
5856
"expiry": "2019-01-09T23:03:16.374062+08:00"
5957
}
60-
```
58+
```
59+
60+
## Password Credentials Grant
61+
62+
Open the browser [http://localhost:9094/pwd](http://localhost:9094/pwd)
63+
64+
```
65+
{
66+
"access_token": "87JT3N6WOWANXVDNZFHY7Q",
67+
"token_type": "Bearer",
68+
"refresh_token": "LDIS6PXAVY-BXHPEDESWNG",
69+
"expiry": "2019-02-12T10:58:43.734902+08:00"
70+
}
71+
```
72+
73+
## Client Credentials Grant
74+
75+
Open the browser [http://localhost:9094/client](http://localhost:9094/client)
76+
77+
```
78+
{
79+
"access_token": "OA6ITALNMDOGD58C0SN-MG",
80+
"token_type": "Bearer",
81+
"expiry": "2019-02-12T11:10:35.864838+08:00"
82+
}
83+
```
84+
85+
![login](https://raw.githubusercontent.com/go-oauth2/oauth2/master/example/server/static/login.png)
86+
![auth](https://raw.githubusercontent.com/go-oauth2/oauth2/master/example/server/static/auth.png)
87+
![token](https://raw.githubusercontent.com/go-oauth2/oauth2/master/example/server/static/token.png)

example/client/client.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"golang.org/x/oauth2"
13+
"golang.org/x/oauth2/clientcredentials"
1314
)
1415

1516
const (
@@ -95,6 +96,37 @@ func main() {
9596
io.Copy(w, resp.Body)
9697
})
9798

99+
http.HandleFunc("/pwd", func(w http.ResponseWriter, r *http.Request) {
100+
token, err := config.PasswordCredentialsToken(context.Background(), "test", "test")
101+
if err != nil {
102+
http.Error(w, err.Error(), http.StatusInternalServerError)
103+
return
104+
}
105+
106+
globalToken = token
107+
e := json.NewEncoder(w)
108+
e.SetIndent("", " ")
109+
e.Encode(token)
110+
})
111+
112+
http.HandleFunc("/client", func(w http.ResponseWriter, r *http.Request) {
113+
cfg := clientcredentials.Config{
114+
ClientID: config.ClientID,
115+
ClientSecret: config.ClientSecret,
116+
TokenURL: config.Endpoint.TokenURL,
117+
}
118+
119+
token, err := cfg.Token(context.Background())
120+
if err != nil {
121+
http.Error(w, err.Error(), http.StatusInternalServerError)
122+
return
123+
}
124+
125+
e := json.NewEncoder(w)
126+
e.SetIndent("", " ")
127+
e.Encode(token)
128+
})
129+
98130
log.Println("Client is running at 9094 port.")
99131
log.Fatal(http.ListenAndServe(":9094", nil))
100132
}

example/server/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ func main() {
3232
manager.MapClientStorage(clientStore)
3333

3434
srv := server.NewServer(server.NewConfig(), manager)
35+
srv.SetPasswordAuthorizationHandler(func(username, password string) (userID string, err error) {
36+
if username == "test" && password == "test" {
37+
userID = "test"
38+
}
39+
return
40+
})
3541
srv.SetUserAuthorizationHandler(userAuthorizeHandler)
3642

3743
srv.SetInternalErrorHandler(func(err error) (re *errors.Response) {

0 commit comments

Comments
 (0)