This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
dependencies.go
106 lines (88 loc) · 2.78 KB
/
dependencies.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package operation
import (
"context"
"net/http"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/ory/hydra-client-go/client/admin"
"golang.org/x/oauth2"
)
type oidcProvider interface {
Name() string
OAuth2Config(scope ...string) oauth2Config
Endpoint() oauth2.Endpoint
Verify(context.Context, string) (idToken, error)
}
type oidcProviderImpl struct {
name string
clientID string
clientSecret string
callback string
skipIssuerCheck bool
op *oidc.Provider
httpClient *http.Client
}
func (o *oidcProviderImpl) Name() string {
return o.name
}
func (o *oidcProviderImpl) OAuth2Config(scope ...string) oauth2Config {
return &oauth2ConfigImpl{
client: o.httpClient,
oc: &oauth2.Config{
ClientID: o.clientID,
ClientSecret: o.clientSecret,
Endpoint: o.op.Endpoint(),
RedirectURL: o.callback,
Scopes: scope,
},
}
}
func (o *oidcProviderImpl) Endpoint() oauth2.Endpoint {
return o.op.Endpoint()
}
func (o *oidcProviderImpl) Verify(ctx context.Context, rawToken string) (idToken, error) {
return o.op.Verifier(&oidc.Config{ClientID: o.clientID, SkipIssuerCheck: o.skipIssuerCheck}).Verify(ctx, rawToken)
}
type idToken interface {
Claims(interface{}) error
}
type oauth2Config interface {
AuthCodeURL(string, ...oauth2.AuthCodeOption) string
Exchange(context.Context, string, ...oauth2.AuthCodeOption) (oauth2Token, error)
}
type oauth2ConfigImpl struct {
oc *oauth2.Config
client *http.Client
}
func (o *oauth2ConfigImpl) AuthCodeURL(state string, options ...oauth2.AuthCodeOption) string {
return o.oc.AuthCodeURL(state, options...)
}
func (o *oauth2ConfigImpl) Exchange(
ctx context.Context, code string, options ...oauth2.AuthCodeOption) (oauth2Token, error) {
return o.oc.Exchange(
context.WithValue(ctx, oauth2.HTTPClient, o.client),
code,
options...,
)
}
type oauth2Token interface {
Extra(string) interface{}
}
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}
// Hydra is the client used to interface with the Hydra service.
type Hydra interface {
GetLoginRequest(params *admin.GetLoginRequestParams, opts ...admin.ClientOption) (*admin.GetLoginRequestOK, error)
AcceptLoginRequest(params *admin.AcceptLoginRequestParams,
opts ...admin.ClientOption) (*admin.AcceptLoginRequestOK, error)
GetConsentRequest(params *admin.GetConsentRequestParams,
opts ...admin.ClientOption) (*admin.GetConsentRequestOK, error)
AcceptConsentRequest(params *admin.AcceptConsentRequestParams,
opts ...admin.ClientOption) (*admin.AcceptConsentRequestOK, error)
IntrospectOAuth2Token(params *admin.IntrospectOAuth2TokenParams,
opts ...admin.ClientOption) (*admin.IntrospectOAuth2TokenOK, error)
}