Files
authorizer/server/oauth/oauth.go

70 lines
2.8 KiB
Go
Raw Normal View History

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