Multiple provider support + OIDC provider
This commit is contained in:
@ -1,16 +1,61 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
// "net/url"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// Providers contains all the implemented providers
|
||||
type Providers struct {
|
||||
Google Google `group:"Google Provider" namespace:"google" env-namespace:"GOOGLE"`
|
||||
OIDC OIDC `group:"OIDC Provider" namespace:"oidc" env-namespace:"OIDC"`
|
||||
}
|
||||
|
||||
type Token struct {
|
||||
// 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)
|
||||
Setup() error
|
||||
}
|
||||
|
||||
type token struct {
|
||||
Token string `json:"access_token"`
|
||||
}
|
||||
|
||||
// User is the authenticated user
|
||||
type User struct {
|
||||
Id string `json:"id"`
|
||||
ID string `json:"id"`
|
||||
Email string `json:"email"`
|
||||
Verified bool `json:"verified_email"`
|
||||
Hd string `json:"hd"`
|
||||
}
|
||||
|
||||
// OAuthProvider is a provider using the oauth2 library
|
||||
type OAuthProvider struct {
|
||||
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)
|
||||
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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user