-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
175 lines (149 loc) · 5.67 KB
/
example_test.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
package oidclient_test
import (
"context"
"fmt"
"log"
"net/http"
"github.com/hooklift/oidclient"
"github.com/hooklift/oidclient/store"
)
func Example_desktop() {
ctx := context.Background()
// 1. Discover provider configuration
provider, err := oidclient.New(ctx, "https://id.hooklift.io", []oidclient.ProviderOption{
oidclient.ClientID("blah"),
oidclient.ClientSecret("blah"),
oidclient.WithSecret("mysecret", "mysecretold"),
oidclient.TokenStore(&store.LocalKeychain{}),
}...)
if err != nil {
log.Fatalf("error retrieving provider's configuration: %v", err)
}
// 2. Start a HTTP server on loopback network interface. It receives authorization code and exchanges it for tokens.
tokensCh := make(chan oidclient.Tokens)
state := oidclient.GenerateNonce()
nonce := oidclient.GenerateNonce()
redirectURI, err := provider.Loopback(ctx, tokensCh, []oidclient.AuthOption{
oidclient.State(state),
oidclient.Nonce(nonce),
}...)
if err != nil {
log.Fatalf("failed to start local HTTP server: %v", err)
}
// 3. Generate provider authorization URL for the user to open in a browser
authURL, err := provider.AuthorizationURL(ctx, []oidclient.AuthOption{
oidclient.RedirectURI(redirectURI),
oidclient.State(state),
oidclient.Nonce(nonce),
oidclient.Scope("profile", "email", "offline"),
}...)
if err != nil {
log.Fatalf("error building authorize URL: %v", err)
}
log.Println("Open the following URL in your browser: %s", authURL)
// 4. OpenID Connect provider authenticates user and asks for consent for "client_blah" to get tokens
// 5. User approves or disapproves
// 6. OpenID Connect Provider sends user back to us through redirectURI with query parameters: code and state, or
// error and error_description.
// 7. HTTP server handler on loopback interface validates state value and exchanges authorization code for tokens
// 8. HTTP server handler validates token signatures, intended audience, state and nonce.
// 9. HTTP server handler returns tokens through the channel
tokens := <-tokensCh
// 10. Check for any errors
if tokens.Error != "" {
log.Fatalf("failed retrieving tokens. %s: %s - %s", tokens.Error, tokens.ErrorDescription, tokens.ErrorURI)
}
// 11. Print out received tokens
log.Println("Tokens: %+v", tokens)
httpClient, err := provider.HTTPClient(ctx, tokens)
}
func Example_mobile() {
ctx := context.Background()
// 1. Discover provider configuration
provider, err := oidclient.New(ctx, "https://id.hooklift.io", []oidclient.ProviderOption{
oidclient.ClientID("client_blah"),
oidclient.ClientSecret("secret_blah"),
oidclient.SkipTLSVerify(),
oidclient.WithSecret("mysecret", "mysecretold"),
oidclient.TokenStore(&store.LocalKeychain{}),
}...)
if err != nil {
log.Fatalf("error retrieving provider's configuration: %v", err)
}
// 2. Get provider authorization URI for the user to open.
state := oidclient.GenerateNonce()
nonce := oidclient.GenerateNonce()
redirectURI := "app.hooklift.flappy:/oauth/callback"
authURL, err := provider.AuthorizationURL(ctx, []oidclient.AuthOption{
oidclient.RedirectURI(redirectURI),
oidclient.Scope("profile", "email", "offline"),
oidclient.State(state),
oidclient.Nonce(nonce),
}...)
if err != nil {
log.Fatalf("error building authorize URL: %v", err)
}
// 3. Native app opens a browser or asks user to follow the authorization URL: authURL
// 4. OpenID Connect provider authenticates user and asks user for consent for "client_blah" to access her resources
// 5. User approves or disapproves
// 6. OpenID Connect Provider redirects user back to us through redirectURI with query parameters: code and state, or
// error and error_description.
// 7. Our Application Delegate gets called with RedirectURI containing the code, state or error and error_description as query parameters
// 8. Retrieves tokens and validates state, nonce, intended audience and token's signatures.
incomingAuthCode := "abasfasdf" // Retrieved from the URL passed by App Delegate to the app
incomingState := "1234"
if state != incomingState {
log.Fatalf("invalid state value")
}
tokens, err := provider.Tokens(ctx, incomingAuthCode, redirectURI)
if err != nil {
log.Fatalf("failed to retrieve tokens: %v", err)
}
log.Println("Tokens: %+v", tokens)
httpClient, err := provider.HTTPClient(ctx, tokens)
}
func Example_web() {
ctx := context.Background()
provider, err := oidclient.New(ctx, "https://id.hooklift.io", []oidclient.ProviderOption{
oidclient.ClientID("blah"),
oidclient.ClientSecret("blah"),
oidclient.SkipTLSVerify(),
oidclient.WithSecret("mysecret", "mysecretold"),
oidclient.TokenStore(&store.Redis{
Address: "localhost:6379",
}),
}...)
if err != nil {
log.Fatal("error retrieving provider's configuration %v", err)
}
mux := http.DefaultServeMux
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
httpClient := oidclient.FromContext(req.Context())
fmt.Fprintf(w, "Hola Mundo!")
})
mux.HandleFunc("/oauth/callback", provider.TokenHandler([]oidclient.AuthOption{}...))
// Initialize authentication handler, it will redirect user to OpenID Connect provider for authentication and consent
// if a httpClient is not found in the request's Context.
handler := provider.Handler(mux, []oidclient.AuthOption{
oidclient.RedirectURI("http://localhost:3000/oauth/callback"),
oidclient.Scope("profile", "email", "offline"),
}...)
srv := &http.Server{
Addr: "localhost:8080",
Handler: handler,
}
done := make(chan bool)
fmt.Printf("Starting server in %q... ", srv.Addr)
go func() {
if err := srv.ListenAndServe(); err != nil {
done <- true
if err != http.ErrServerClosed {
panic(err)
}
} else {
done <- true
}
}()
<-done
fmt.Println("done")
}