2021-07-17 16:29:50 +00:00
|
|
|
package oauth
|
|
|
|
|
|
|
|
import (
|
2021-12-03 17:25:27 +00:00
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2021-12-03 17:25:27 +00:00
|
|
|
"github.com/coreos/go-oidc/v3/oidc"
|
2021-07-17 16:29:50 +00:00
|
|
|
"golang.org/x/oauth2"
|
2021-09-04 22:27:29 +00:00
|
|
|
facebookOAuth2 "golang.org/x/oauth2/facebook"
|
2021-07-17 16:29:50 +00:00
|
|
|
githubOAuth2 "golang.org/x/oauth2/github"
|
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// OAuthProviders is a struct that contains reference all the OAuth providers
|
2021-12-03 17:25:27 +00:00
|
|
|
type OAuthProvider struct {
|
2021-09-04 22:27:29 +00:00
|
|
|
GoogleConfig *oauth2.Config
|
|
|
|
GithubConfig *oauth2.Config
|
|
|
|
FacebookConfig *oauth2.Config
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// OIDCProviders is a struct that contains reference all the OpenID providers
|
2021-12-03 17:25:27 +00:00
|
|
|
type OIDCProvider struct {
|
|
|
|
GoogleOIDC *oidc.Provider
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-01-17 06:02:13 +00:00
|
|
|
// OAuthProviders is a global variable that contains instance for all enabled the OAuth providers
|
2021-12-03 17:25:27 +00:00
|
|
|
OAuthProviders OAuthProvider
|
2022-01-17 06:02:13 +00:00
|
|
|
// OIDCProviders is a global variable that contains instance for all enabled the OpenID providers
|
|
|
|
OIDCProviders OIDCProvider
|
2021-12-03 17:25:27 +00:00
|
|
|
)
|
2021-07-17 16:29:50 +00:00
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// InitOAuth initializes the OAuth providers based on EnvData
|
2021-07-28 11:08:55 +00:00
|
|
|
func InitOAuth() {
|
2021-12-03 17:25:27 +00:00
|
|
|
ctx := context.Background()
|
2022-01-17 06:02:13 +00:00
|
|
|
if envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyGoogleClientID).(string) != "" && envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyGoogleClientSecret).(string) != "" {
|
2021-12-03 17:25:27 +00:00
|
|
|
p, err := oidc.NewProvider(ctx, "https://accounts.google.com")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("error creating oidc provider for google:", err)
|
|
|
|
}
|
|
|
|
OIDCProviders.GoogleOIDC = p
|
|
|
|
OAuthProviders.GoogleConfig = &oauth2.Config{
|
2022-01-17 06:02:13 +00:00
|
|
|
ClientID: envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyGoogleClientID).(string),
|
|
|
|
ClientSecret: envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyGoogleClientSecret).(string),
|
|
|
|
RedirectURL: envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyAuthorizerURL).(string) + "/oauth_callback/google",
|
2021-12-03 17:25:27 +00:00
|
|
|
Endpoint: OIDCProviders.GoogleOIDC.Endpoint(),
|
|
|
|
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 06:02:13 +00:00
|
|
|
if envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyGithubClientID).(string) != "" && envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyGithubClientSecret).(string) != "" {
|
2021-12-03 17:25:27 +00:00
|
|
|
OAuthProviders.GithubConfig = &oauth2.Config{
|
2022-01-17 06:02:13 +00:00
|
|
|
ClientID: envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyGithubClientID).(string),
|
|
|
|
ClientSecret: envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyGithubClientSecret).(string),
|
|
|
|
RedirectURL: envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyAuthorizerURL).(string) + "/oauth_callback/github",
|
2021-07-17 16:29:50 +00:00
|
|
|
Endpoint: githubOAuth2.Endpoint,
|
|
|
|
}
|
|
|
|
}
|
2022-01-17 06:02:13 +00:00
|
|
|
if envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyFacebookClientID).(string) != "" && envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyGoogleClientID).(string) != "" {
|
2021-12-03 17:25:27 +00:00
|
|
|
OAuthProviders.FacebookConfig = &oauth2.Config{
|
2022-01-17 06:02:13 +00:00
|
|
|
ClientID: envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyFacebookClientID).(string),
|
|
|
|
ClientSecret: envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyFacebookClientSecret).(string),
|
|
|
|
RedirectURL: envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyAuthorizerURL).(string) + "/oauth_callback/facebook",
|
2021-09-04 22:27:29 +00:00
|
|
|
Endpoint: facebookOAuth2.Endpoint,
|
|
|
|
Scopes: []string{"public_profile", "email"},
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|