-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
53 lines (44 loc) · 1.18 KB
/
auth.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
package infra
import (
"context"
"errors"
"github.com/coreos/go-oidc/v3/oidc"
"golang.org/x/oauth2"
)
// Authenticator is used to authenticate our users.
type Authenticator struct {
*oidc.Provider
oauth2.Config
}
// InitAuthenticator instantiates the *Authenticator.
func InitAuthenticator(authConf Auth0Conf) (*Authenticator, error) {
provider, err := oidc.NewProvider(
context.Background(),
"https://"+authConf.Domain+"/",
)
if err != nil {
return nil, err
}
conf := oauth2.Config{
ClientID: authConf.ClientID,
ClientSecret: authConf.ClientSecret,
RedirectURL: authConf.CallbackURL,
Endpoint: provider.Endpoint(),
Scopes: []string{oidc.ScopeOpenID, "profile"},
}
return &Authenticator{
Provider: provider,
Config: conf,
}, nil
}
// VerifyIDToken verifies that an *oauth2.Token is a valid *oidc.IDToken.
func (a *Authenticator) VerifyIDToken(ctx context.Context, token *oauth2.Token) (*oidc.IDToken, error) {
rawIDToken, ok := token.Extra("id_token").(string)
if !ok {
return nil, errors.New("no id_token field in oauth2 token")
}
oidcConfig := &oidc.Config{
ClientID: a.ClientID,
}
return a.Verifier(oidcConfig).Verify(ctx, rawIDToken)
}