-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviders.go
139 lines (112 loc) · 4.21 KB
/
providers.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package auth
import (
"fmt"
"github.com/KowalskiPiotr98/ludivault/utils"
"github.com/markbates/goth"
"github.com/markbates/goth/providers/discord"
"github.com/markbates/goth/providers/gitea"
"github.com/markbates/goth/providers/github"
"github.com/markbates/goth/providers/google"
"github.com/markbates/goth/providers/openidConnect"
"github.com/markbates/goth/providers/steam"
"github.com/markbates/goth/providers/twitch"
log "github.com/sirupsen/logrus"
"strings"
)
var (
providers []string
setups = map[string]func(callbackUrl string) (bool, goth.Provider){
"gitea": func(callbackUrl string) (bool, goth.Provider) {
clientId := utils.GetOptionalConfig("SSO_GITEA_CLIENT_ID", "")
clientSecret := utils.GetOptionalConfig("SSO_GITEA_CLIENT_SECRET", "")
url := utils.GetOptionalConfig("SSO_GITEA_URL", "")
if clientId == "" || clientSecret == "" || url == "" {
return false, nil
}
url = strings.TrimRight(url, "/")
return true, gitea.NewCustomisedURL(clientId, clientSecret, callbackUrl, fmt.Sprintf("%s/login/oauth/authorize", url), fmt.Sprintf("%s/login/oauth/access_token", url), fmt.Sprintf("%s/api/v1/user", url))
},
"twitch": func(callbackUrl string) (bool, goth.Provider) {
clientId := utils.GetOptionalConfig("SSO_TWITCH_CLIENT_ID", "")
clientSecret := utils.GetOptionalConfig("SSO_TWITCH_CLIENT_SECRET", "")
if clientId == "" || clientSecret == "" {
return false, nil
}
return true, twitch.New(clientId, clientSecret, callbackUrl)
},
"github": func(callbackUrl string) (bool, goth.Provider) {
clientId := utils.GetOptionalConfig("SSO_GITHUB_CLIENT_ID", "")
clientSecret := utils.GetOptionalConfig("SSO_GITHUB_CLIENT_SECRET", "")
if clientId == "" || clientSecret == "" {
return false, nil
}
return true, github.New(clientId, clientSecret, callbackUrl)
},
"discord": func(callbackUrl string) (bool, goth.Provider) {
clientId := utils.GetOptionalConfig("SSO_DISCORD_CLIENT_ID", "")
clientSecret := utils.GetOptionalConfig("SSO_DISCORD_CLIENT_SECRET", "")
if clientId == "" || clientSecret == "" {
return false, nil
}
return true, discord.New(clientId, clientSecret, callbackUrl)
},
"google": func(callbackUrl string) (bool, goth.Provider) {
clientId := utils.GetOptionalConfig("SSO_GOOGLE_CLIENT_ID", "")
clientSecret := utils.GetOptionalConfig("SSO_GOOGLE_CLIENT_SECRET", "")
if clientId == "" || clientSecret == "" {
return false, nil
}
return true, google.New(clientId, clientSecret, callbackUrl)
},
"steam": func(callbackUrl string) (bool, goth.Provider) {
clientSecret := utils.GetOptionalConfig("SSO_STEAM_CLIENT_SECRET", "")
if clientSecret == "" {
return false, nil
}
return true, steam.New(clientSecret, callbackUrl)
},
"oidc": func(callbackUrl string) (bool, goth.Provider) {
clientId := utils.GetOptionalConfig("SSO_OIDC_CLIENT_ID", "")
clientSecret := utils.GetOptionalConfig("SSO_OIDC_CLIENT_SECRET", "")
discoveryUrl := utils.GetOptionalConfig("SSO_OIDC_DISCOVERY_URL", "")
if clientId == "" || clientSecret == "" || discoveryUrl == "" {
return false, nil
}
provider, err := openidConnect.New(clientId, clientSecret, discoveryUrl, callbackUrl)
if err != nil {
log.Warnf("Failed to initialize OpenID Connect provider: %v", err)
return false, nil
}
return true, provider
},
}
)
func SetupProviders(baseUrl string) error {
if areProvidersSet() {
// prevent duplicate provider setup
return ProvidersAlreadyInitialised
}
baseUrl = strings.TrimRight(baseUrl, "/")
callbackUrl := fmt.Sprintf("%s/api/v1/auth/callback?provider=%%s", baseUrl)
log.Debugf("Setting provider callback url to: %s", callbackUrl)
enabledProviders := make([]goth.Provider, 0)
for providerName, setup := range setups {
ok, provider := setup(fmt.Sprintf(callbackUrl, providerName))
if ok {
providers = append(providers, providerName)
enabledProviders = append(enabledProviders, provider)
log.Debugf("Registered login provider %s", providerName)
}
}
goth.UseProviders(enabledProviders...)
if !areProvidersSet() {
return NoProvidersSet
}
return nil
}
func GetEnabledProviders() []string {
return providers
}
func areProvidersSet() bool {
return len(providers) > 0
}