-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
73 lines (60 loc) · 1.39 KB
/
options.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
package oauth2
import (
"fmt"
"net/url"
"path"
"strings"
"golang.org/x/oauth2"
)
// Options for Oauth2 strategy
type Options struct {
// Unique name for strategy
Name string
// URL for server
URL string
// Oauth2 options
ClientID string
ClientSecret string
Scopes []string
Endpoint oauth2.Endpoint
}
func required(key, value string) error {
if value == "" {
return fmt.Errorf("%s is a required option for oauth2.Strategy", key)
}
return nil
}
const reservedChars = ";,/"
// Validate checks if the options are valid
func (o Options) Validate() error {
if err := required("Name", o.Name); err != nil {
return err
}
if err := required("URL", o.URL); err != nil {
return err
}
if strings.ContainsAny(o.Name, reservedChars) {
return fmt.Errorf("Name may not contain characters: %s", reservedChars)
}
u, err := url.Parse(o.URL)
if err != nil || u.Hostname() == "" {
return fmt.Errorf("URL must be valid")
}
return nil
}
// AuthPath returns the auth path
func (o Options) AuthPath() string {
return path.Join("/oauth2", o.Name, "auth")
}
// AuthURL returns the auth URL
func (o Options) AuthURL() string {
return o.URL + o.AuthPath()
}
// LoginPath returns the login path
func (o Options) LoginPath() string {
return path.Join("/oauth2", o.Name, "login")
}
// LoginURL returns the login URL
func (o Options) LoginURL() string {
return o.URL + o.LoginPath()
}