-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (139 loc) · 4.38 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
This is an example application to demonstrate querying the user info endpoint.
*/
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"fmt"
oidc "github.com/coreos/go-oidc"
"github.com/google/uuid"
"github.com/thomasdarimont/go-kc-example/session"
_ "github.com/thomasdarimont/go-kc-example/session_memory"
"golang.org/x/net/context"
"golang.org/x/oauth2"
)
var (
clientID = os.Getenv("CLIENT_ID")
clientSecret = os.Getenv("CLIENT_SECRET")
)
const oauth_state = "oauth_state"
type GoUserInfo struct {
userID string
email string
username string
displayName string
}
var globalSessions *session.Manager
// Then, initialize the session manager
func init() {
globalSessions, _ = session.NewManager("memory", "gosessionid", 3600)
go globalSessions.GC()
}
func main() {
ctx := context.Background()
provider, err := oidc.NewProvider(ctx, "http://localhost:8081/auth/realms/godemo")
if err != nil {
log.Fatal(err)
}
oidcConfig := &oidc.Config{
ClientID: clientID,
}
config := &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: provider.Endpoint(),
RedirectURL: "http://127.0.0.1:5556/auth/keycloak/callback",
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
}
verifier := provider.Verifier(oidcConfig)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
sess := globalSessions.SessionStart(w, r)
oauthState := uuid.New().String()
sess.Set(oauth_state, oauthState)
userInfo := sess.Get("userinfo")
if userInfo == nil {
http.Redirect(w, r, config.AuthCodeURL(oauthState), http.StatusFound)
return
}
http.Redirect(w, r, "/app", http.StatusFound)
})
http.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
sess := globalSessions.SessionStart(w, r)
userInfo := sess.Get("userinfo").(*GoUserInfo)
fmt.Fprintf(w, "Welcome: %s\n", userInfo)
})
http.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
sess := globalSessions.SessionStart(w, r)
oauthState := uuid.New().String()
sess.Set(oauth_state, oauthState)
userInfo := sess.Get("userinfo")
if userInfo == nil {
http.Redirect(w, r, config.AuthCodeURL(oauthState), http.StatusFound)
return
}
fmt.Printf("logout %s\n", userInfo.(*GoUserInfo).userID)
rawIDtoken := sess.Get("rawIDToken").(string)
redirect, err := provider.Logout(ctx, nil, rawIDtoken, "http://127.0.0.1:5556/", oauthState) //TODO handle state token on logout
if err != nil {
http.Error(w, "state did not match", http.StatusBadRequest)
return
}
globalSessions.SessionDestroy(w, r)
http.Redirect(w, r, redirect, http.StatusFound)
})
http.HandleFunc("/auth/keycloak/callback", func(w http.ResponseWriter, r *http.Request) {
sess := globalSessions.SessionStart(w, r)
oauthState := sess.Get(oauth_state)
if oauthState == nil {
http.Error(w, "state did not match", http.StatusBadRequest)
return
}
if r.URL.Query().Get("state") != oauthState.(string) {
http.Error(w, "state did not match", http.StatusBadRequest)
return
}
oauth2Token, err := config.Exchange(ctx, r.URL.Query().Get("code"))
if err != nil {
http.Error(w, "Failed to exchange token: "+err.Error(), http.StatusInternalServerError)
return
}
userInfo, err := provider.UserInfo(ctx, oauth2.StaticTokenSource(oauth2Token))
if err != nil {
http.Error(w, "Failed to get userinfo: "+err.Error(), http.StatusInternalServerError)
return
}
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
http.Error(w, "No id_token field in oauth2 token.", http.StatusInternalServerError)
return
}
_, err = verifier.Verify(ctx, rawIDToken)
if err != nil {
http.Error(w, "Failed to verify ID Token: "+err.Error(), http.StatusInternalServerError)
return
}
sess.Set("userinfo", &GoUserInfo{
userID: userInfo.Subject,
username: userInfo.PreferredUsername,
displayName: userInfo.Name,
email: userInfo.Email,
})
sess.Set("rawIDToken", rawIDToken)
resp := struct {
OAuth2Token *oauth2.Token
UserInfo *oidc.UserInfo
}{oauth2Token, userInfo}
data, err := json.MarshalIndent(resp, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(data)
})
log.Printf("listening on http://%s/", "127.0.0.1:5556")
log.Fatal(http.ListenAndServe("127.0.0.1:5556", nil))
}