-
Notifications
You must be signed in to change notification settings - Fork 128
/
password.go
43 lines (36 loc) · 905 Bytes
/
password.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
package oidc
import (
"context"
openId "github.com/coreos/go-oidc"
"golang.org/x/oauth2"
)
type ClientPasswordDetails struct {
ProviderUrl string
ClientId string
Scopes []string
Username string
Password string
}
func AuthenticateWithPassword(config ClientPasswordDetails) (*TokenCredentials, error) {
ctx := context.Background()
provider, err := openId.NewProvider(ctx, config.ProviderUrl)
if err != nil {
return nil, err
}
authConfig := &oauth2.Config{
ClientID: config.ClientId,
Scopes: config.Scopes,
Endpoint: provider.Endpoint(),
}
source := &FunctionTokenSource{
func() (*oauth2.Token, error) {
return authConfig.PasswordCredentialsToken(ctx, config.Username, config.Password)
},
}
t, err := source.Token()
if err != nil {
return nil, err
}
cachedSource := oauth2.ReuseTokenSource(t, source)
return &TokenCredentials{cachedSource}, nil
}