mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-31 22:25:43 +00:00
refactor: return all values from body in the providers
This commit is contained in:
@@ -613,14 +613,23 @@ func (h *Handlers) OauthCallbackHandler(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get email
|
// Get user
|
||||||
email, err := h.Providers.GetUser(providerName.Provider)
|
user, err := h.Providers.GetUser(providerName.Provider)
|
||||||
|
|
||||||
log.Debug().Str("email", email).Msg("Got email")
|
|
||||||
|
|
||||||
// Handle error
|
// Handle error
|
||||||
if err != nil {
|
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))
|
c.Redirect(http.StatusPermanentRedirect, fmt.Sprintf("%s/error", h.Config.AppURL))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,18 +8,16 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// We are assuming that the generic provider will return a JSON object with an email field
|
func GetGenericUser(client *http.Client, url string) (map[string]interface{}, error) {
|
||||||
type GenericUserInfoResponse struct {
|
// Create user struct
|
||||||
Email string `json:"email"`
|
user := make(map[string]interface{})
|
||||||
}
|
|
||||||
|
|
||||||
func GetGenericEmail(client *http.Client, url string) (string, error) {
|
|
||||||
// Using the oauth client get the user info url
|
// Using the oauth client get the user info url
|
||||||
res, err := client.Get(url)
|
res, err := client.Get(url)
|
||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Got response from generic provider")
|
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
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Read body from generic provider")
|
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
|
// Unmarshal the body into the user struct
|
||||||
err = json.Unmarshal(body, &user)
|
err = json.Unmarshal(body, &user)
|
||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Parsed user from generic provider")
|
log.Debug().Msg("Parsed user from generic provider")
|
||||||
|
|
||||||
// Return the email
|
// Return the user
|
||||||
return user.Email, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,13 +20,16 @@ func GithubScopes() []string {
|
|||||||
return []string{"user:email"}
|
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
|
// Get the user emails from github using the oauth http client
|
||||||
res, err := client.Get("https://api.github.com/user/emails")
|
res, err := client.Get("https://api.github.com/user/emails")
|
||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Got response from github")
|
log.Debug().Msg("Got response from github")
|
||||||
@@ -36,7 +39,7 @@ func GetGithubEmail(client *http.Client) (string, error) {
|
|||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Read body from github")
|
log.Debug().Msg("Read body from github")
|
||||||
@@ -49,7 +52,7 @@ func GetGithubEmail(client *http.Client) (string, error) {
|
|||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Parsed emails from github")
|
log.Debug().Msg("Parsed emails from github")
|
||||||
@@ -57,10 +60,11 @@ func GetGithubEmail(client *http.Client) (string, error) {
|
|||||||
// Find and return the primary email
|
// Find and return the primary email
|
||||||
for _, email := range emails {
|
for _, email := range emails {
|
||||||
if email.Primary {
|
if email.Primary {
|
||||||
return email.Email, nil
|
user["email"] = email.Email
|
||||||
|
return user, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// User does not have a primary email?
|
// User does not have a primary email?
|
||||||
return "", errors.New("no primary email found")
|
return user, errors.New("no primary email found")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,23 +8,21 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"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
|
// The scopes required for the google provider
|
||||||
func GoogleScopes() []string {
|
func GoogleScopes() []string {
|
||||||
return []string{"https://www.googleapis.com/auth/userinfo.email"}
|
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
|
// Get the user info from google using the oauth http client
|
||||||
res, err := client.Get("https://www.googleapis.com/userinfo/v2/me")
|
res, err := client.Get("https://www.googleapis.com/userinfo/v2/me")
|
||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Got response from google")
|
log.Debug().Msg("Got response from google")
|
||||||
@@ -34,24 +32,21 @@ func GetGoogleEmail(client *http.Client) (string, error) {
|
|||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Read body from google")
|
log.Debug().Msg("Read body from google")
|
||||||
|
|
||||||
// Parse the body into a user struct
|
|
||||||
var user GoogleUserInfoResponse
|
|
||||||
|
|
||||||
// Unmarshal the body into the user struct
|
// Unmarshal the body into the user struct
|
||||||
err = json.Unmarshal(body, &user)
|
err = json.Unmarshal(body, &user)
|
||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("Parsed user from google")
|
log.Debug().Msg("Parsed user from google")
|
||||||
|
|
||||||
// Return the email
|
// Return the user
|
||||||
return user.Email, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,14 +93,17 @@ func (providers *Providers) GetProvider(provider string) *oauth.OAuth {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (providers *Providers) GetUser(provider string) (string, error) {
|
func (providers *Providers) GetUser(provider string) (map[string]interface{}, error) {
|
||||||
// Get the email from the provider
|
// Create user struct
|
||||||
|
user := make(map[string]interface{})
|
||||||
|
|
||||||
|
// Get the user from the provider
|
||||||
switch provider {
|
switch provider {
|
||||||
case "github":
|
case "github":
|
||||||
// If the github provider is not configured, return an error
|
// If the github provider is not configured, return an error
|
||||||
if providers.Github == nil {
|
if providers.Github == nil {
|
||||||
log.Debug().Msg("Github provider not configured")
|
log.Debug().Msg("Github provider not configured")
|
||||||
return "", nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the client from the github provider
|
// 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")
|
log.Debug().Msg("Got client from github")
|
||||||
|
|
||||||
// Get the email from the github provider
|
// Get the user from the github provider
|
||||||
email, err := GetGithubEmail(client)
|
user, err := GetGithubUser(client)
|
||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
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 the user
|
||||||
return email, nil
|
return user, nil
|
||||||
case "google":
|
case "google":
|
||||||
// If the google provider is not configured, return an error
|
// If the google provider is not configured, return an error
|
||||||
if providers.Google == nil {
|
if providers.Google == nil {
|
||||||
log.Debug().Msg("Google provider not configured")
|
log.Debug().Msg("Google provider not configured")
|
||||||
return "", nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the client from the google provider
|
// 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")
|
log.Debug().Msg("Got client from google")
|
||||||
|
|
||||||
// Get the email from the google provider
|
// Get the user from the google provider
|
||||||
email, err := GetGoogleEmail(client)
|
user, err := GetGoogleUser(client)
|
||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
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 the user
|
||||||
return email, nil
|
return user, nil
|
||||||
case "generic":
|
case "generic":
|
||||||
// If the generic provider is not configured, return an error
|
// If the generic provider is not configured, return an error
|
||||||
if providers.Generic == nil {
|
if providers.Generic == nil {
|
||||||
log.Debug().Msg("Generic provider not configured")
|
log.Debug().Msg("Generic provider not configured")
|
||||||
return "", nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the client from the generic provider
|
// 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")
|
log.Debug().Msg("Got client from generic")
|
||||||
|
|
||||||
// Get the email from the generic provider
|
// Get the user from the generic provider
|
||||||
email, err := GetGenericEmail(client, providers.Config.GenericUserURL)
|
user, err := GetGenericUser(client, providers.Config.GenericUserURL)
|
||||||
|
|
||||||
// Check if there was an error
|
// Check if there was an error
|
||||||
if err != nil {
|
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 the email
|
||||||
return email, nil
|
return user, nil
|
||||||
default:
|
default:
|
||||||
return "", nil
|
return user, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user