From 13032e564d10ab85c23061124dc1e9117b9a602a Mon Sep 17 00:00:00 2001 From: Stavros Date: Fri, 25 Apr 2025 15:02:34 +0300 Subject: [PATCH] refactor: return all values from body in the providers --- internal/handlers/handlers.go | 19 +++++++++---- internal/providers/generic.go | 21 ++++++-------- internal/providers/github.go | 16 +++++++---- internal/providers/google.go | 23 ++++++---------- internal/providers/providers.go | 49 +++++++++++++++++---------------- 5 files changed, 67 insertions(+), 61 deletions(-) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 6e80851..c873d9f 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -613,14 +613,23 @@ func (h *Handlers) OauthCallbackHandler(c *gin.Context) { return } - // Get email - email, err := h.Providers.GetUser(providerName.Provider) - - log.Debug().Str("email", email).Msg("Got email") + // Get user + user, err := h.Providers.GetUser(providerName.Provider) // Handle error if err != nil { - log.Error().Msg("Failed to get email") + log.Error().Msg("Failed to get user") + c.Redirect(http.StatusPermanentRedirect, fmt.Sprintf("%s/error", h.Config.AppURL)) + return + } + + log.Debug().Msg("Got user") + + // Get email + email, ok := user["email"].(string) + + if !ok { + log.Error().Msg("Failed to get email from user") c.Redirect(http.StatusPermanentRedirect, fmt.Sprintf("%s/error", h.Config.AppURL)) return } diff --git a/internal/providers/generic.go b/internal/providers/generic.go index 798b039..084d5fb 100644 --- a/internal/providers/generic.go +++ b/internal/providers/generic.go @@ -8,18 +8,16 @@ import ( "github.com/rs/zerolog/log" ) -// We are assuming that the generic provider will return a JSON object with an email field -type GenericUserInfoResponse struct { - Email string `json:"email"` -} +func GetGenericUser(client *http.Client, url string) (map[string]interface{}, error) { + // Create user struct + user := make(map[string]interface{}) -func GetGenericEmail(client *http.Client, url string) (string, error) { // Using the oauth client get the user info url res, err := client.Get(url) // Check if there was an error if err != nil { - return "", err + return user, err } log.Debug().Msg("Got response from generic provider") @@ -29,24 +27,21 @@ func GetGenericEmail(client *http.Client, url string) (string, error) { // Check if there was an error if err != nil { - return "", err + return user, err } log.Debug().Msg("Read body from generic provider") - // Parse the body into a user struct - var user GenericUserInfoResponse - // Unmarshal the body into the user struct err = json.Unmarshal(body, &user) // Check if there was an error if err != nil { - return "", err + return user, err } log.Debug().Msg("Parsed user from generic provider") - // Return the email - return user.Email, nil + // Return the user + return user, nil } diff --git a/internal/providers/github.go b/internal/providers/github.go index 010e799..3c1a59c 100644 --- a/internal/providers/github.go +++ b/internal/providers/github.go @@ -20,13 +20,16 @@ func GithubScopes() []string { return []string{"user:email"} } -func GetGithubEmail(client *http.Client) (string, error) { +func GetGithubUser(client *http.Client) (map[string]interface{}, error) { + // Create user struct + user := make(map[string]interface{}) + // Get the user emails from github using the oauth http client res, err := client.Get("https://api.github.com/user/emails") // Check if there was an error if err != nil { - return "", err + return user, err } log.Debug().Msg("Got response from github") @@ -36,7 +39,7 @@ func GetGithubEmail(client *http.Client) (string, error) { // Check if there was an error if err != nil { - return "", err + return user, err } log.Debug().Msg("Read body from github") @@ -49,7 +52,7 @@ func GetGithubEmail(client *http.Client) (string, error) { // Check if there was an error if err != nil { - return "", err + return user, err } log.Debug().Msg("Parsed emails from github") @@ -57,10 +60,11 @@ func GetGithubEmail(client *http.Client) (string, error) { // Find and return the primary email for _, email := range emails { if email.Primary { - return email.Email, nil + user["email"] = email.Email + return user, nil } } // User does not have a primary email? - return "", errors.New("no primary email found") + return user, errors.New("no primary email found") } diff --git a/internal/providers/google.go b/internal/providers/google.go index ba5c8b4..9785586 100644 --- a/internal/providers/google.go +++ b/internal/providers/google.go @@ -8,23 +8,21 @@ import ( "github.com/rs/zerolog/log" ) -// Google works the same as the generic provider -type GoogleUserInfoResponse struct { - Email string `json:"email"` -} - // The scopes required for the google provider func GoogleScopes() []string { return []string{"https://www.googleapis.com/auth/userinfo.email"} } -func GetGoogleEmail(client *http.Client) (string, error) { +func GetGoogleUser(client *http.Client) (map[string]interface{}, error) { + // Create user struct + user := make(map[string]interface{}) + // Get the user info from google using the oauth http client res, err := client.Get("https://www.googleapis.com/userinfo/v2/me") // Check if there was an error if err != nil { - return "", err + return user, err } log.Debug().Msg("Got response from google") @@ -34,24 +32,21 @@ func GetGoogleEmail(client *http.Client) (string, error) { // Check if there was an error if err != nil { - return "", err + return user, err } log.Debug().Msg("Read body from google") - // Parse the body into a user struct - var user GoogleUserInfoResponse - // Unmarshal the body into the user struct err = json.Unmarshal(body, &user) // Check if there was an error if err != nil { - return "", err + return user, err } log.Debug().Msg("Parsed user from google") - // Return the email - return user.Email, nil + // Return the user + return user, nil } diff --git a/internal/providers/providers.go b/internal/providers/providers.go index c1bad5e..3c4f4fc 100644 --- a/internal/providers/providers.go +++ b/internal/providers/providers.go @@ -93,14 +93,17 @@ func (providers *Providers) GetProvider(provider string) *oauth.OAuth { } } -func (providers *Providers) GetUser(provider string) (string, error) { - // Get the email from the provider +func (providers *Providers) GetUser(provider string) (map[string]interface{}, error) { + // Create user struct + user := make(map[string]interface{}) + + // Get the user from the provider switch provider { case "github": // If the github provider is not configured, return an error if providers.Github == nil { log.Debug().Msg("Github provider not configured") - return "", nil + return user, nil } // Get the client from the github provider @@ -108,23 +111,23 @@ func (providers *Providers) GetUser(provider string) (string, error) { log.Debug().Msg("Got client from github") - // Get the email from the github provider - email, err := GetGithubEmail(client) + // Get the user from the github provider + user, err := GetGithubUser(client) // Check if there was an error if err != nil { - return "", err + return user, err } - log.Debug().Msg("Got email from github") + log.Debug().Msg("Got user from github") - // Return the email - return email, nil + // Return the user + return user, nil case "google": // If the google provider is not configured, return an error if providers.Google == nil { log.Debug().Msg("Google provider not configured") - return "", nil + return user, nil } // Get the client from the google provider @@ -132,23 +135,23 @@ func (providers *Providers) GetUser(provider string) (string, error) { log.Debug().Msg("Got client from google") - // Get the email from the google provider - email, err := GetGoogleEmail(client) + // Get the user from the google provider + user, err := GetGoogleUser(client) // Check if there was an error if err != nil { - return "", err + return user, err } - log.Debug().Msg("Got email from google") + log.Debug().Msg("Got user from google") - // Return the email - return email, nil + // Return the user + return user, nil case "generic": // If the generic provider is not configured, return an error if providers.Generic == nil { log.Debug().Msg("Generic provider not configured") - return "", nil + return user, nil } // Get the client from the generic provider @@ -156,20 +159,20 @@ func (providers *Providers) GetUser(provider string) (string, error) { log.Debug().Msg("Got client from generic") - // Get the email from the generic provider - email, err := GetGenericEmail(client, providers.Config.GenericUserURL) + // Get the user from the generic provider + user, err := GetGenericUser(client, providers.Config.GenericUserURL) // Check if there was an error if err != nil { - return "", err + return user, err } - log.Debug().Msg("Got email from generic") + log.Debug().Msg("Got user from generic") // Return the email - return email, nil + return user, nil default: - return "", nil + return user, nil } }