-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_builder.go
69 lines (57 loc) · 1.47 KB
/
request_builder.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
package gauth
import (
"errors"
"github.com/LeoInnovateLab/gauth/config"
)
type AuthRequestFactory interface {
NewAuthRequest(*config.AuthConfig) (AuthRequest, error)
}
var factories = make(map[string]AuthRequestFactory)
func Register(source string, factory AuthRequestFactory) {
factories[source] = factory
}
type AuthRequestBuilder struct {
source string
clientId string
clientSecret string
redirectUrl string
}
func New() *AuthRequestBuilder {
return &AuthRequestBuilder{}
}
func (b *AuthRequestBuilder) Source(source string) *AuthRequestBuilder {
b.source = source
return b
}
func (b *AuthRequestBuilder) ClientId(clientId string) *AuthRequestBuilder {
b.clientId = clientId
return b
}
func (b *AuthRequestBuilder) ClientSecret(clientSecret string) *AuthRequestBuilder {
b.clientSecret = clientSecret
return b
}
func (b *AuthRequestBuilder) RedirectUrl(redirectUrl string) *AuthRequestBuilder {
b.redirectUrl = redirectUrl
return b
}
func (b *AuthRequestBuilder) Build() (AuthRequest, error) {
if b.clientId == "" {
return nil, ErrClientIdNotFound
}
if b.source == "" {
return nil, ErrSourceNotFound
}
if b.clientSecret == "" {
return nil, ErrClientSecretNotFound
}
factory, ok := factories[b.source]
if !ok {
return nil, errors.New("source not supported yet")
}
return factory.NewAuthRequest(config.NewAuthConfig(
config.WithClientId(b.clientId),
config.WithClientSecret(b.clientSecret),
config.WithRedirectUrl(b.redirectUrl),
))
}