-
Notifications
You must be signed in to change notification settings - Fork 578
/
Copy pathclient.go
141 lines (121 loc) · 3.53 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
const (
authServerURL = "http://localhost:9096"
)
var (
config = oauth2.Config{
ClientID: "222222",
ClientSecret: "22222222",
Scopes: []string{"all"},
RedirectURL: "http://localhost:9094/oauth2",
Endpoint: oauth2.Endpoint{
AuthURL: authServerURL + "/oauth/authorize",
TokenURL: authServerURL + "/oauth/token",
},
}
globalToken *oauth2.Token // Non-concurrent security
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
u := config.AuthCodeURL("xyz",
oauth2.SetAuthURLParam("code_challenge", genCodeChallengeS256("s256example")),
oauth2.SetAuthURLParam("code_challenge_method", "S256"))
http.Redirect(w, r, u, http.StatusFound)
})
http.HandleFunc("/oauth2", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
state := r.Form.Get("state")
if state != "xyz" {
http.Error(w, "State invalid", http.StatusBadRequest)
return
}
code := r.Form.Get("code")
if code == "" {
http.Error(w, "Code not found", http.StatusBadRequest)
return
}
token, err := config.Exchange(context.Background(), code, oauth2.SetAuthURLParam("code_verifier", "s256example"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
globalToken = token
e := json.NewEncoder(w)
e.SetIndent("", " ")
e.Encode(token)
})
http.HandleFunc("/refresh", func(w http.ResponseWriter, r *http.Request) {
if globalToken == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
globalToken.Expiry = time.Now()
token, err := config.TokenSource(context.Background(), globalToken).Token()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
globalToken = token
e := json.NewEncoder(w)
e.SetIndent("", " ")
e.Encode(token)
})
http.HandleFunc("/try", func(w http.ResponseWriter, r *http.Request) {
if globalToken == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
resp, err := http.Get(fmt.Sprintf("%s/test?access_token=%s", authServerURL, globalToken.AccessToken))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer resp.Body.Close()
io.Copy(w, resp.Body)
})
http.HandleFunc("/pwd", func(w http.ResponseWriter, r *http.Request) {
token, err := config.PasswordCredentialsToken(context.Background(), "test", "test")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
globalToken = token
e := json.NewEncoder(w)
e.SetIndent("", " ")
e.Encode(token)
})
http.HandleFunc("/client", func(w http.ResponseWriter, r *http.Request) {
cfg := clientcredentials.Config{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
TokenURL: config.Endpoint.TokenURL,
}
token, err := cfg.Token(context.Background())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
e := json.NewEncoder(w)
e.SetIndent("", " ")
e.Encode(token)
})
log.Println("Client is running at 9094 port.Please open http://localhost:9094")
log.Fatal(http.ListenAndServe(":9094", nil))
}
func genCodeChallengeS256(s string) string {
s256 := sha256.Sum256([]byte(s))
return base64.RawURLEncoding.EncodeToString(s256[:])
}