feat: google oauth

This commit is contained in:
Stavros
2025-01-24 16:29:21 +02:00
parent 433e71bd50
commit d4eca52b12
4 changed files with 88 additions and 29 deletions

View File

@@ -65,8 +65,7 @@ var rootCmd = &cobra.Command{
GithubClientSecret: config.GithubClientSecret,
GoogleClientId: config.GoogleClientId,
GoogleClientSecret: config.GoogleClientSecret,
MicrosoftClientId: config.MicrosoftClientId,
MicrosoftClientSecret: config.MicrosoftClientSecret,
AppURL: config.AppURL,
}
// Create auth service
@@ -128,8 +127,6 @@ func init() {
rootCmd.Flags().String("github-client-secret", "", "Github OAuth client secret.")
rootCmd.Flags().String("google-client-id", "", "Google OAuth client ID.")
rootCmd.Flags().String("google-client-secret", "", "Google OAuth client secret.")
rootCmd.Flags().String("microsoft-client-id", "", "Microsoft OAuth client ID.")
rootCmd.Flags().String("microsoft-client-secret", "", "Microsoft OAuth client secret.")
viper.BindEnv("port", "PORT")
viper.BindEnv("address", "ADDRESS")
viper.BindEnv("secret", "SECRET")
@@ -141,7 +138,5 @@ func init() {
viper.BindEnv("github-client-secret", "GITHUB_CLIENT_SECRET")
viper.BindEnv("google-client-id", "GOOGLE_CLIENT_ID")
viper.BindEnv("google-client-secret", "GOOGLE_CLIENT_SECRET")
viper.BindEnv("microsoft-client-id", "MICROSOFT_CLIENT_ID")
viper.BindEnv("microsoft-client-secret", "MICROSOFT_CLIENT_SECRET")
viper.BindPFlags(rootCmd.Flags())
}

View File

@@ -0,0 +1,39 @@
package providers
import (
"encoding/json"
"io"
"net/http"
)
type GoogleUserinfoResponse struct {
Email string `json:"email"`
}
func GoogleScopes() []string {
return []string{"https://www.googleapis.com/auth/userinfo.email"}
}
func GetGoogleEmail(client *http.Client) (string, error) {
res, resErr := client.Get("https://www.googleapis.com/userinfo/v2/me")
if resErr != nil {
return "", resErr
}
body, bodyErr := io.ReadAll(res.Body)
if bodyErr != nil {
return "", bodyErr
}
var user GoogleUserinfoResponse
jsonErr := json.Unmarshal(body, &user)
if jsonErr != nil {
return "", jsonErr
}
return user.Email, nil
}

View File

@@ -1,6 +1,7 @@
package providers
import (
"fmt"
"tinyauth/internal/oauth"
"tinyauth/internal/types"
@@ -28,17 +29,31 @@ func (providers *Providers) Init() {
providers.Github = oauth.NewOAuth(oauth2.Config{
ClientID: providers.Config.GithubClientId,
ClientSecret: providers.Config.GithubClientSecret,
RedirectURL: fmt.Sprintf("%s/api/oauth/callback/github", providers.Config.AppURL),
Scopes: GithubScopes(),
Endpoint: endpoints.GitHub,
})
providers.Github.Init()
}
if providers.Config.GoogleClientId != "" && providers.Config.GoogleClientSecret != "" {
log.Info().Msg("Initializing Google OAuth")
providers.Google = oauth.NewOAuth(oauth2.Config{
ClientID: providers.Config.GoogleClientId,
ClientSecret: providers.Config.GoogleClientSecret,
RedirectURL: fmt.Sprintf("%s/api/oauth/callback/google", providers.Config.AppURL),
Scopes: GoogleScopes(),
Endpoint: endpoints.Google,
})
providers.Google.Init()
}
}
func (providers *Providers) GetProvider(provider string) *oauth.OAuth {
switch provider {
case "github":
return providers.Github
case "google":
return providers.Google
default:
return nil
}
@@ -56,6 +71,16 @@ func (providers *Providers) GetUser(provider string) (string, error) {
return "", emailErr
}
return email, nil
case "google":
if providers.Google == nil {
return "", nil
}
client := providers.Google.GetClient()
email, emailErr := GetGoogleEmail(client)
if emailErr != nil {
return "", emailErr
}
return email, nil
default:
return "", nil
}
@@ -66,5 +91,8 @@ func (provider *Providers) GetConfiguredProviders() []string {
if provider.Github != nil {
providers = append(providers, "github")
}
if provider.Google != nil {
providers = append(providers, "google")
}
return providers
}

View File

@@ -30,8 +30,6 @@ type Config struct {
GithubClientSecret string `mapstructure:"github-client-secret"`
GoogleClientId string `mapstructure:"google-client-id"`
GoogleClientSecret string `mapstructure:"google-client-secret"`
MicrosoftClientId string `mapstructure:"microsoft-client-id"`
MicrosoftClientSecret string `mapstructure:"microsoft-client-secret"`
}
type UserContext struct {
@@ -54,8 +52,7 @@ type OAuthConfig struct {
GithubClientSecret string
GoogleClientId string
GoogleClientSecret string
MicrosoftClientId string
MicrosoftClientSecret string
AppURL string
}
type OAuthRequest struct {