feat: generic oauth

This commit is contained in:
Stavros
2025-01-24 17:13:51 +02:00
parent f487e25ac5
commit 90f4c3c980
8 changed files with 165 additions and 33 deletions

View File

@@ -17,10 +17,10 @@ func NewProviders(config types.OAuthConfig) *Providers {
}
type Providers struct {
Config types.OAuthConfig
Github *oauth.OAuth
Google *oauth.OAuth
Microsoft *oauth.OAuth
Config types.OAuthConfig
Github *oauth.OAuth
Google *oauth.OAuth
Generic *oauth.OAuth
}
func (providers *Providers) Init() {
@@ -46,6 +46,20 @@ func (providers *Providers) Init() {
})
providers.Google.Init()
}
if providers.Config.GenericClientId != "" && providers.Config.GenericClientSecret != "" {
log.Info().Msg("Initializing Generic OAuth")
providers.Generic = oauth.NewOAuth(oauth2.Config{
ClientID: providers.Config.GenericClientId,
ClientSecret: providers.Config.GenericClientSecret,
RedirectURL: fmt.Sprintf("%s/api/oauth/callback/generic", providers.Config.AppURL),
Scopes: []string{providers.Config.GenericScopes},
Endpoint: oauth2.Endpoint{
AuthURL: providers.Config.GenericAuthURL,
TokenURL: providers.Config.GenericTokenURL,
},
})
providers.Generic.Init()
}
}
func (providers *Providers) GetProvider(provider string) *oauth.OAuth {
@@ -54,6 +68,8 @@ func (providers *Providers) GetProvider(provider string) *oauth.OAuth {
return providers.Github
case "google":
return providers.Google
case "generic":
return providers.Generic
default:
return nil
}
@@ -81,6 +97,16 @@ func (providers *Providers) GetUser(provider string) (string, error) {
return "", emailErr
}
return email, nil
case "generic":
if providers.Generic == nil {
return "", nil
}
client := providers.Generic.GetClient()
email, emailErr := GetGenericEmail(client, providers.Config.GenericUserInfoURL)
if emailErr != nil {
return "", emailErr
}
return email, nil
default:
return "", nil
}
@@ -94,5 +120,8 @@ func (provider *Providers) GetConfiguredProviders() []string {
if provider.Google != nil {
providers = append(providers, "google")
}
if provider.Generic != nil {
providers = append(providers, "generic")
}
return providers
}