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