-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoauth.go
71 lines (51 loc) · 1.47 KB
/
goauth.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
package goauth
type onAuthFunction func(authCode string)
type OAuth struct {
// The client ID used within the OAuth protocol.
ClientId string
// The client secret used within the OAuth protocol.
ClientSecret string
// Space-delimited list of APIs you wish to access
Scope string
// OAuth for the local server that starts.
ServerConfig ServerConfig
provider oAuthProvider
error error
}
// Configuration for the local server that starts to handle the OAuth process
type ServerConfig struct {
// The port to start the local server on. Defaults to port 80.
Port int
// The URL that the local server will redirect to upon auth success.
AuthFailedUrl string
// The URL that the local server will redirect to upon auth failure.
AuthSuccessUrl string
oAuthUrl string
redirectUri string
}
// Google OAuth 2
func Google(oAuth OAuth) *GoogleOAuth {
oAuth.provider = GOOGLE
if err := validateConfig(&oAuth); err != nil {
oAuth.error = err
}
return &GoogleOAuth{OAuth: &oAuth}
}
func validateConfig(config *OAuth) error {
if config.ClientId == "" {
return InvalidParameterError{Msg: "Client ID is required for OAuth"}
}
if config.ClientSecret == "" {
return InvalidParameterError{Msg: "Client secret is required for OAuth"}
}
if config.Scope == "" {
return InvalidParameterError{Msg: "A scope is required for OAuth"}
}
if config.provider == "" {
config.provider = GOOGLE
}
if config.ServerConfig.Port == 0 {
config.ServerConfig.Port = 80
}
return nil
}