/
credentials.go
66 lines (56 loc) · 1.54 KB
/
credentials.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
package go_shopware_admin_sdk
import (
"context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
type OAuthCredentials interface {
GetTokenSource(ctx context.Context, tokenURL string) (oauth2.TokenSource, error)
}
type PasswordCredentials struct {
Username string
Password string
Scopes []string
}
func NewPasswordCredentials(username, password string, scopes []string) PasswordCredentials {
return PasswordCredentials{
Username: username,
Password: password,
Scopes: scopes,
}
}
func (c PasswordCredentials) GetTokenSource(ctx context.Context, tokenURL string) (oauth2.TokenSource, error) {
oauthConf := &oauth2.Config{
ClientID: "administration",
Scopes: c.Scopes,
Endpoint: oauth2.Endpoint{
TokenURL: tokenURL,
},
}
token, err := oauthConf.PasswordCredentialsToken(ctx, c.Username, c.Password)
if err != nil {
return nil, err
}
return oauth2.StaticTokenSource(token), nil
}
type IntegrationCredentials struct {
ClientId string
ClientSecret string
Scopes []string
}
func NewIntegrationCredentials(clientId, clientSecret string, scopes []string) IntegrationCredentials {
return IntegrationCredentials{
ClientId: clientId,
ClientSecret: clientSecret,
Scopes: scopes,
}
}
func (c IntegrationCredentials) GetTokenSource(ctx context.Context, tokenURL string) (oauth2.TokenSource, error) {
oauthConf := &clientcredentials.Config{
ClientID: c.ClientId,
ClientSecret: c.ClientSecret,
Scopes: c.Scopes,
TokenURL: tokenURL,
}
return oauthConf.TokenSource(ctx), nil
}