-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
170 lines (146 loc) · 4.68 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
package main
import (
"context"
"flag"
"fmt"
"github.com/go-chi/chi"
"github.com/toqueteos/webbrowser"
"golang.org/x/oauth2"
"html/template"
"net/http"
"strings"
"time"
)
var indexPage = template.Must(template.New("").Parse(`<html>
<body>
<h1>Crossid Samples</h1>
<ul>
<li><a href="{{ .URL }}">Login using code flow</a></li>
</body>
</html>
`))
var errorPage = template.Must(template.New("").Parse(`<html>
<body>
<h1>An error occurred</h1>
<h2>{{ .Name }}</h2>
<p>{{ .Description }}</p>
</body>
</html>
`))
var tokenUserResult = template.Must(template.New("").Parse(`<html>
<html>
<head></head>
<body>
<h1>Success!</h1>
<ul>
<li>Access Token: <code>{{ .AccessToken }}</code></li>
<li>Expires at: <code>{{ .Expiry }}</code></li>
<li>Refresh Token: <code>{{ .RefreshToken }}</code></li>
<li>ID Token: <code>{{ .IDToken }}</code></li>
</ul>
</body>
</html>`))
func main() {
portPtr := flag.Int("port", 3000, "local app port")
issuerBaseURLPtr := flag.String("issuerBaseURL", "https://demo.crossid.io/api/v1/oauth2/authorization-servers/default", "Authorization Server base url")
scopesPtr := flag.String("scope", "openid offline profile", "Requested scopes")
redirectURLPtr := flag.String("redirect-url", "https://localhost/callback", "where to redirect after login")
clientIDPtr := flag.String("client-id", "sample", "the registered client id in the authorization server")
clientSecretPtr := flag.String("client-secret", "super-secret", "the registered client secret in the authorization server")
audiencePtr := flag.String("audience", "https://api.example.com/products", "requested audience")
promptPtr := flag.String("prompt", "consent", "consent or none")
flag.Parse()
externalLocation := "localhost"
listenOn := fmt.Sprintf("%s:%d", externalLocation, *portPtr)
conf := oauth2.Config{
ClientID: *clientIDPtr,
ClientSecret: *clientSecretPtr,
Endpoint: oauth2.Endpoint{
TokenURL: *issuerBaseURLPtr + "/token",
AuthURL: *issuerBaseURLPtr + "/auth",
},
RedirectURL: *redirectURLPtr,
Scopes: strings.Split(*scopesPtr, " "),
}
state := RandomString(30)
nonce := RandomString(30)
authCodeURL := conf.AuthCodeURL(
state,
oauth2.SetAuthURLParam("audience", *audiencePtr),
oauth2.SetAuthURLParam("nonce", nonce),
//oauth2.SetAuthURLParam("max_age", *maxAge),
oauth2.SetAuthURLParam("prompt", *promptPtr),
)
_ = webbrowser.Open("https://" + externalLocation)
r := chi.NewRouter()
server := &http.Server{Addr: listenOn, Handler: r}
var shutdown = func() {
time.Sleep(time.Second * 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
_ = server.Shutdown(ctx)
}
fmt.Println("listening on " + listenOn)
fmt.Println("callback url https://" + externalLocation + "/callback")
fmt.Printf("if browser is not opened automatically, navigate to: %s\n", "https://"+externalLocation)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
_ = indexPage.Execute(w, &struct{ URL string }{URL: authCodeURL})
})
type derr struct {
Name string
Description string
Hint string
Debug string
}
r.Get("/callback", func(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Query().Get("error")) > 0 {
fmt.Printf("Got error: %s\n", r.URL.Query().Get("error_description"))
w.WriteHeader(http.StatusInternalServerError)
_ = errorPage.Execute(w, &derr{
Name: r.URL.Query().Get("error"),
Description: r.URL.Query().Get("error_description"),
Hint: r.URL.Query().Get("error_hint"),
Debug: r.URL.Query().Get("error_debug"),
})
go shutdown()
return
}
if r.URL.Query().Get("state") != state {
fmt.Printf("states mismatch. Expected %s, got %s\n", state, r.URL.Query().Get("state"))
w.WriteHeader(http.StatusInternalServerError)
_ = errorPage.Execute(w, &derr{
Name: "states mismatch",
Description: "Expected state " + state + ", got " + r.URL.Query().Get("state"),
})
go shutdown()
return
}
code := r.URL.Query().Get("code")
token, err := conf.Exchange(context.Background(), code)
if err != nil {
fmt.Printf("Unable to exchange code for token: %s\n", err)
w.WriteHeader(http.StatusInternalServerError)
_ = errorPage.Execute(w, &derr{
Name: err.Error(),
})
go shutdown()
return
}
idt := token.Extra("id_token")
_ = tokenUserResult.Execute(w, struct {
AccessToken string
RefreshToken string
IDToken string
Expiry string
}{
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
Expiry: token.Expiry.Format(time.RFC1123),
IDToken: fmt.Sprintf("%v", idt),
})
go shutdown()
})
if err := server.ListenAndServe(); err != nil {
fmt.Println(err)
}
}