2021-07-17 16:29:50 +00:00
|
|
|
package oauth
|
|
|
|
|
|
|
|
import (
|
2021-12-03 17:25:27 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"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-06-06 16:38:32 +00:00
|
|
|
linkedInOAuth2 "golang.org/x/oauth2/linkedin"
|
2022-05-24 07:12:29 +00:00
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-05-29 11:52:46 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2021-07-17 16:29:50 +00:00
|
|
|
)
|
|
|
|
|
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
|
2022-06-06 16:38:32 +00:00
|
|
|
LinkedInConfig *oauth2.Config
|
2022-06-12 09:19:48 +00:00
|
|
|
AppleConfig *oauth2.Config
|
2022-08-13 07:05:00 +00:00
|
|
|
TwitterConfig *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
|
2022-02-26 04:36:26 +00:00
|
|
|
func InitOAuth() error {
|
2021-12-03 17:25:27 +00:00
|
|
|
ctx := context.Background()
|
2022-05-29 11:52:46 +00:00
|
|
|
googleClientID, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyGoogleClientID)
|
|
|
|
if err != nil {
|
|
|
|
googleClientID = ""
|
|
|
|
}
|
|
|
|
googleClientSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyGoogleClientSecret)
|
|
|
|
if err != nil {
|
|
|
|
googleClientSecret = ""
|
|
|
|
}
|
|
|
|
if googleClientID != "" && googleClientSecret != "" {
|
2021-12-03 17:25:27 +00:00
|
|
|
p, err := oidc.NewProvider(ctx, "https://accounts.google.com")
|
|
|
|
if err != nil {
|
2022-02-26 04:36:26 +00:00
|
|
|
return err
|
2021-12-03 17:25:27 +00:00
|
|
|
}
|
|
|
|
OIDCProviders.GoogleOIDC = p
|
|
|
|
OAuthProviders.GoogleConfig = &oauth2.Config{
|
2022-05-29 11:52:46 +00:00
|
|
|
ClientID: googleClientID,
|
|
|
|
ClientSecret: googleClientSecret,
|
2022-01-31 06:05:24 +00:00
|
|
|
RedirectURL: "/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-05-29 11:52:46 +00:00
|
|
|
|
|
|
|
githubClientID, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyGithubClientID)
|
|
|
|
if err != nil {
|
|
|
|
githubClientID = ""
|
|
|
|
}
|
|
|
|
githubClientSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyGithubClientSecret)
|
|
|
|
if err != nil {
|
|
|
|
githubClientSecret = ""
|
|
|
|
}
|
|
|
|
if githubClientID != "" && githubClientSecret != "" {
|
2021-12-03 17:25:27 +00:00
|
|
|
OAuthProviders.GithubConfig = &oauth2.Config{
|
2022-05-29 11:52:46 +00:00
|
|
|
ClientID: githubClientID,
|
|
|
|
ClientSecret: githubClientSecret,
|
2022-01-31 06:05:24 +00:00
|
|
|
RedirectURL: "/oauth_callback/github",
|
2021-07-17 16:29:50 +00:00
|
|
|
Endpoint: githubOAuth2.Endpoint,
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 11:52:46 +00:00
|
|
|
|
|
|
|
facebookClientID, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyFacebookClientID)
|
|
|
|
if err != nil {
|
|
|
|
facebookClientID = ""
|
|
|
|
}
|
|
|
|
facebookClientSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyFacebookClientSecret)
|
|
|
|
if err != nil {
|
|
|
|
facebookClientSecret = ""
|
|
|
|
}
|
|
|
|
if facebookClientID != "" && facebookClientSecret != "" {
|
2021-12-03 17:25:27 +00:00
|
|
|
OAuthProviders.FacebookConfig = &oauth2.Config{
|
2022-05-29 11:52:46 +00:00
|
|
|
ClientID: facebookClientID,
|
|
|
|
ClientSecret: facebookClientSecret,
|
2022-01-31 06:05:24 +00:00
|
|
|
RedirectURL: "/oauth_callback/facebook",
|
2021-09-04 22:27:29 +00:00
|
|
|
Endpoint: facebookOAuth2.Endpoint,
|
|
|
|
Scopes: []string{"public_profile", "email"},
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 04:36:26 +00:00
|
|
|
|
2022-06-06 16:38:32 +00:00
|
|
|
linkedInClientID, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyLinkedInClientID)
|
|
|
|
if err != nil {
|
|
|
|
linkedInClientID = ""
|
|
|
|
}
|
|
|
|
linkedInClientSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyLinkedInClientSecret)
|
|
|
|
if err != nil {
|
|
|
|
linkedInClientSecret = ""
|
|
|
|
}
|
|
|
|
if linkedInClientID != "" && linkedInClientSecret != "" {
|
|
|
|
OAuthProviders.LinkedInConfig = &oauth2.Config{
|
|
|
|
ClientID: linkedInClientID,
|
|
|
|
ClientSecret: linkedInClientSecret,
|
|
|
|
RedirectURL: "/oauth_callback/linkedin",
|
|
|
|
Endpoint: linkedInOAuth2.Endpoint,
|
|
|
|
Scopes: []string{"r_liteprofile", "r_emailaddress"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 09:19:48 +00:00
|
|
|
appleClientID, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAppleClientID)
|
|
|
|
if err != nil {
|
|
|
|
appleClientID = ""
|
|
|
|
}
|
|
|
|
appleClientSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyAppleClientSecret)
|
|
|
|
if err != nil {
|
|
|
|
appleClientSecret = ""
|
|
|
|
}
|
|
|
|
if appleClientID != "" && appleClientSecret != "" {
|
|
|
|
OAuthProviders.AppleConfig = &oauth2.Config{
|
|
|
|
ClientID: appleClientID,
|
2022-06-14 05:41:09 +00:00
|
|
|
ClientSecret: appleClientSecret,
|
2022-06-12 09:19:48 +00:00
|
|
|
RedirectURL: "/oauth_callback/apple",
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
AuthURL: "https://appleid.apple.com/auth/authorize",
|
|
|
|
TokenURL: "https://appleid.apple.com/auth/token",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 18:19:48 +00:00
|
|
|
twitterClientID, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyTwitterClientID)
|
|
|
|
if err != nil {
|
|
|
|
twitterClientID = ""
|
|
|
|
}
|
|
|
|
twitterClientSecret, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyTwitterClientSecret)
|
|
|
|
if err != nil {
|
|
|
|
twitterClientSecret = ""
|
|
|
|
}
|
|
|
|
if twitterClientID != "" && twitterClientSecret != "" {
|
|
|
|
OAuthProviders.TwitterConfig = &oauth2.Config{
|
|
|
|
ClientID: twitterClientID,
|
|
|
|
ClientSecret: twitterClientSecret,
|
|
|
|
RedirectURL: "/oauth_callback/twitter",
|
|
|
|
Endpoint: oauth2.Endpoint{
|
|
|
|
// Endpoint is currently not yet part of oauth2-package. See https://go-review.googlesource.com/c/oauth2/+/350889 for status
|
|
|
|
AuthURL: "https://twitter.com/i/oauth2/authorize",
|
|
|
|
TokenURL: "https://api.twitter.com/2/oauth2/token",
|
|
|
|
AuthStyle: oauth2.AuthStyleInHeader,
|
|
|
|
},
|
|
|
|
Scopes: []string{"tweet.read", "users.read"},
|
|
|
|
}
|
|
|
|
}
|
2022-08-13 07:05:00 +00:00
|
|
|
|
2022-02-26 04:36:26 +00:00
|
|
|
return nil
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|