-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (45 loc) · 1.34 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
package main
import (
"context"
"fmt"
"log"
"os"
"go.n0stack.dev/lib/openid/configuration"
oidc "go.n0stack.dev/lib/openid/connect"
"go.n0stack.dev/lib/openid/connect/authenticator"
"go.n0stack.dev/lib/openid/examples"
)
func AuthorizationCodeExample(ctx context.Context) (*oidc.Token, error) {
conf, err := configuration.Fetch(ctx, examples.ExampleIssuer)
if err != nil {
return nil, fmt.Errorf(`configuration.Fetch(ctx, auth.OIDC_ISSUER) %w`, err)
}
authz := authenticator.Begin(conf).PublicClient(examples.ClientPublic).AuthorizationCodeGrant(authenticator.OOBRedirectURI, authenticator.PKCE_CODE_CHALLENGE_METHOD_S256)
authCodeURL, err := authz.AuthCodeURL()
if err != nil {
return nil, fmt.Errorf(`code.AuthCodeURL() %w`, err)
}
fmt.Fprintf(os.Stdout, "Open the following link in your browser:\n\n\t%s\n\n", authCodeURL)
fmt.Printf("Enter verification code: ")
code := ""
if _, err := fmt.Scan(&code); err != nil {
return nil, err
}
ts, err := authz.Exchange(ctx, code, authenticator.SkipStateCheck)
if err != nil {
return nil, err
}
token, err := ts.Token(ctx)
if err != nil {
return nil, err
}
return token, nil
}
func main() {
ctx := context.Background()
token, err := AuthorizationCodeExample(ctx)
if err != nil {
log.Fatalf(`AuthorizationCodeExample(ctx) %v`, err)
}
log.Printf("Got token %v", token)
}