68 lines
1.9 KiB
Go
Raw Normal View History

2019-01-30 16:52:47 +00:00
package provider
import (
"context"
// "net/url"
"golang.org/x/oauth2"
2023-11-06 18:15:03 +01:00
"github.com/sirupsen/logrus"
)
// Providers contains all the implemented providers
2019-01-30 16:52:47 +00:00
type Providers struct {
2020-06-29 21:04:42 +01:00
Google Google `group:"Google Provider" namespace:"google" env-namespace:"GOOGLE"`
OIDC OIDC `group:"OIDC Provider" namespace:"oidc" env-namespace:"OIDC"`
GenericOAuth GenericOAuth `group:"Generic OAuth2 Provider" namespace:"generic-oauth" env-namespace:"GENERIC_OAUTH"`
}
// Provider is used to authenticate users
type Provider interface {
Name() string
GetLoginURL(redirectURI, state string) string
ExchangeCode(redirectURI, code string) (string, error)
GetUser(token string) (User, error)
2023-11-06 18:15:03 +01:00
Setup(*logrus.Logger) error
2019-01-30 16:52:47 +00:00
}
type token struct {
2019-01-30 16:52:47 +00:00
Token string `json:"access_token"`
}
// User is the authenticated user
2019-01-30 16:52:47 +00:00
type User struct {
Email string `json:"email"`
2019-01-30 16:52:47 +00:00
}
// OAuthProvider is a provider using the oauth2 library
type OAuthProvider struct {
Resource string `long:"resource" env:"RESOURCE" description:"Optional resource indicator"`
Config *oauth2.Config
ctx context.Context
}
// ConfigCopy returns a copy of the oauth2 config with the given redirectURI
// which ensures the underlying config is not modified
func (p *OAuthProvider) ConfigCopy(redirectURI string) oauth2.Config {
config := *p.Config
config.RedirectURL = redirectURI
return config
}
// OAuthGetLoginURL provides a base "GetLoginURL" for proiders using OAauth2
func (p *OAuthProvider) OAuthGetLoginURL(redirectURI, state string) string {
config := p.ConfigCopy(redirectURI)
if p.Resource != "" {
return config.AuthCodeURL(state, oauth2.SetAuthURLParam("resource", p.Resource))
}
return config.AuthCodeURL(state)
}
// OAuthExchangeCode provides a base "ExchangeCode" for proiders using OAauth2
func (p *OAuthProvider) OAuthExchangeCode(redirectURI, code string) (*oauth2.Token, error) {
config := p.ConfigCopy(redirectURI)
return config.Exchange(p.ctx, code)
}