mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-11-03 23:55:44 +00:00 
			
		
		
		
	feat: map info from OIDC claims to headers (#122)
* refactor: return all values from body in the providers * refactor: only accept claims following the OIDC spec * feat: map info from OIDC claims to headers * feat: add support for required oauth groups * fix: bot suggestions * feat: get claims from github and google * fix: close body correctly
This commit is contained in:
		@@ -1,22 +1,27 @@
 | 
			
		||||
package hooks
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"tinyauth/internal/auth"
 | 
			
		||||
	"tinyauth/internal/providers"
 | 
			
		||||
	"tinyauth/internal/types"
 | 
			
		||||
	"tinyauth/internal/utils"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"github.com/rs/zerolog/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func NewHooks(auth *auth.Auth, providers *providers.Providers) *Hooks {
 | 
			
		||||
func NewHooks(config types.HooksConfig, auth *auth.Auth, providers *providers.Providers) *Hooks {
 | 
			
		||||
	return &Hooks{
 | 
			
		||||
		Config:    config,
 | 
			
		||||
		Auth:      auth,
 | 
			
		||||
		Providers: providers,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Hooks struct {
 | 
			
		||||
	Config    types.HooksConfig
 | 
			
		||||
	Auth      *auth.Auth
 | 
			
		||||
	Providers *providers.Providers
 | 
			
		||||
}
 | 
			
		||||
@@ -36,11 +41,11 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
 | 
			
		||||
		if user != nil && hooks.Auth.CheckPassword(*user, basic.Password) {
 | 
			
		||||
			// Return user context since we are logged in with basic auth
 | 
			
		||||
			return types.UserContext{
 | 
			
		||||
				Username:    basic.Username,
 | 
			
		||||
				IsLoggedIn:  true,
 | 
			
		||||
				OAuth:       false,
 | 
			
		||||
				Provider:    "basic",
 | 
			
		||||
				TotpPending: false,
 | 
			
		||||
				Username:   basic.Username,
 | 
			
		||||
				Name:       utils.Capitalize(basic.Username),
 | 
			
		||||
				Email:      fmt.Sprintf("%s@%s", strings.ToLower(basic.Username), hooks.Config.Domain),
 | 
			
		||||
				IsLoggedIn: true,
 | 
			
		||||
				Provider:   "basic",
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -50,13 +55,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error().Err(err).Msg("Failed to get session cookie")
 | 
			
		||||
		// Return empty context
 | 
			
		||||
		return types.UserContext{
 | 
			
		||||
			Username:    "",
 | 
			
		||||
			IsLoggedIn:  false,
 | 
			
		||||
			OAuth:       false,
 | 
			
		||||
			Provider:    "",
 | 
			
		||||
			TotpPending: false,
 | 
			
		||||
		}
 | 
			
		||||
		return types.UserContext{}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check if session cookie has totp pending
 | 
			
		||||
@@ -65,8 +64,8 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
 | 
			
		||||
		// Return empty context since we are pending totp
 | 
			
		||||
		return types.UserContext{
 | 
			
		||||
			Username:    cookie.Username,
 | 
			
		||||
			IsLoggedIn:  false,
 | 
			
		||||
			OAuth:       false,
 | 
			
		||||
			Name:        cookie.Name,
 | 
			
		||||
			Email:       cookie.Email,
 | 
			
		||||
			Provider:    cookie.Provider,
 | 
			
		||||
			TotpPending: true,
 | 
			
		||||
		}
 | 
			
		||||
@@ -82,11 +81,11 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
 | 
			
		||||
 | 
			
		||||
			// It exists so we are logged in
 | 
			
		||||
			return types.UserContext{
 | 
			
		||||
				Username:    cookie.Username,
 | 
			
		||||
				IsLoggedIn:  true,
 | 
			
		||||
				OAuth:       false,
 | 
			
		||||
				Provider:    "username",
 | 
			
		||||
				TotpPending: false,
 | 
			
		||||
				Username:   cookie.Username,
 | 
			
		||||
				Name:       cookie.Name,
 | 
			
		||||
				Email:      cookie.Email,
 | 
			
		||||
				IsLoggedIn: true,
 | 
			
		||||
				Provider:   "username",
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@@ -108,13 +107,7 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
 | 
			
		||||
			hooks.Auth.DeleteSessionCookie(c)
 | 
			
		||||
 | 
			
		||||
			// Return empty context
 | 
			
		||||
			return types.UserContext{
 | 
			
		||||
				Username:    "",
 | 
			
		||||
				IsLoggedIn:  false,
 | 
			
		||||
				OAuth:       false,
 | 
			
		||||
				Provider:    "",
 | 
			
		||||
				TotpPending: false,
 | 
			
		||||
			}
 | 
			
		||||
			return types.UserContext{}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		log.Debug().Msg("Email is whitelisted")
 | 
			
		||||
@@ -122,19 +115,15 @@ func (hooks *Hooks) UseUserContext(c *gin.Context) types.UserContext {
 | 
			
		||||
		// Return user context since we are logged in with oauth
 | 
			
		||||
		return types.UserContext{
 | 
			
		||||
			Username:    cookie.Username,
 | 
			
		||||
			Name:        cookie.Name,
 | 
			
		||||
			Email:       cookie.Email,
 | 
			
		||||
			IsLoggedIn:  true,
 | 
			
		||||
			OAuth:       true,
 | 
			
		||||
			Provider:    cookie.Provider,
 | 
			
		||||
			TotpPending: false,
 | 
			
		||||
			OAuthGroups: cookie.OAuthGroups,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Neither basic auth or oauth is set so we return an empty context
 | 
			
		||||
	return types.UserContext{
 | 
			
		||||
		Username:    "",
 | 
			
		||||
		IsLoggedIn:  false,
 | 
			
		||||
		OAuth:       false,
 | 
			
		||||
		Provider:    "",
 | 
			
		||||
		TotpPending: false,
 | 
			
		||||
	}
 | 
			
		||||
	return types.UserContext{}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user