mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-10-28 04:35:40 +00:00 
			
		
		
		
	feat: add regex support to oauth whitelist
This commit is contained in:
		| @@ -61,13 +61,6 @@ var rootCmd = &cobra.Command{ | |||||||
| 			HandleError(errors.New("no users or OAuth configured"), "No users or OAuth configured") | 			HandleError(errors.New("no users or OAuth configured"), "No users or OAuth configured") | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// Create oauth whitelist |  | ||||||
| 		oauthWhitelist := utils.Filter(strings.Split(config.OAuthWhitelist, ","), func(val string) bool { |  | ||||||
| 			return val != "" |  | ||||||
| 		}) |  | ||||||
|  |  | ||||||
| 		log.Debug().Msg("Parsed OAuth whitelist") |  | ||||||
|  |  | ||||||
| 		// Get domain | 		// Get domain | ||||||
| 		log.Debug().Msg("Getting domain") | 		log.Debug().Msg("Getting domain") | ||||||
| 		domain, err := utils.GetUpperDomain(config.AppURL) | 		domain, err := utils.GetUpperDomain(config.AppURL) | ||||||
| @@ -108,7 +101,7 @@ var rootCmd = &cobra.Command{ | |||||||
| 		// Create auth config | 		// Create auth config | ||||||
| 		authConfig := types.AuthConfig{ | 		authConfig := types.AuthConfig{ | ||||||
| 			Users:           users, | 			Users:           users, | ||||||
| 			OauthWhitelist:  oauthWhitelist, | 			OauthWhitelist:  config.OAuthWhitelist, | ||||||
| 			Secret:          config.Secret, | 			Secret:          config.Secret, | ||||||
| 			CookieSecure:    config.CookieSecure, | 			CookieSecure:    config.CookieSecure, | ||||||
| 			SessionExpiry:   config.SessionExpiry, | 			SessionExpiry:   config.SessionExpiry, | ||||||
|   | |||||||
| @@ -35,7 +35,7 @@ export const UnauthorizedPage = () => { | |||||||
|           ) : ( |           ) : ( | ||||||
|             <Text> |             <Text> | ||||||
|               <Trans |               <Trans | ||||||
|                 i18nKey="unauthorizedLoginSubtitle" |                 i18nKey="unaothorizedLoginSubtitle" | ||||||
|                 t={t} |                 t={t} | ||||||
|                 components={{ Code: <Code /> }} |                 components={{ Code: <Code /> }} | ||||||
|                 values={{ username }} |                 values={{ username }} | ||||||
|   | |||||||
| @@ -36,7 +36,7 @@ var handlersConfig = types.HandlersConfig{ | |||||||
| // Simple auth config for tests | // Simple auth config for tests | ||||||
| var authConfig = types.AuthConfig{ | var authConfig = types.AuthConfig{ | ||||||
| 	Users:           types.Users{}, | 	Users:           types.Users{}, | ||||||
| 	OauthWhitelist:  []string{}, | 	OauthWhitelist:  "", | ||||||
| 	Secret:          "super-secret-api-thing-for-tests", // It is 32 chars long | 	Secret:          "super-secret-api-thing-for-tests", // It is 32 chars long | ||||||
| 	CookieSecure:    false, | 	CookieSecure:    false, | ||||||
| 	SessionExpiry:   3600, | 	SessionExpiry:   3600, | ||||||
|   | |||||||
| @@ -134,20 +134,7 @@ func (auth *Auth) RecordLoginAttempt(identifier string, success bool) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func (auth *Auth) EmailWhitelisted(emailSrc string) bool { | func (auth *Auth) EmailWhitelisted(emailSrc string) bool { | ||||||
| 	// If the whitelist is empty, allow all emails | 	return utils.CheckWhitelist(auth.Config.OauthWhitelist, emailSrc) | ||||||
| 	if len(auth.Config.OauthWhitelist) == 0 { |  | ||||||
| 		return true |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// Loop through the whitelist and return true if the email matches |  | ||||||
| 	for _, email := range auth.Config.OauthWhitelist { |  | ||||||
| 		if email == emailSrc { |  | ||||||
| 			return true |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	// If no emails match, return false |  | ||||||
| 	return false |  | ||||||
| } | } | ||||||
|  |  | ||||||
| func (auth *Auth) CreateSessionCookie(c *gin.Context, data *types.SessionCookie) error { | func (auth *Auth) CreateSessionCookie(c *gin.Context, data *types.SessionCookie) error { | ||||||
|   | |||||||
| @@ -10,7 +10,7 @@ import ( | |||||||
|  |  | ||||||
| var config = types.AuthConfig{ | var config = types.AuthConfig{ | ||||||
| 	Users:          types.Users{}, | 	Users:          types.Users{}, | ||||||
| 	OauthWhitelist: []string{}, | 	OauthWhitelist: "", | ||||||
| 	SessionExpiry:  3600, | 	SessionExpiry:  3600, | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -68,7 +68,7 @@ type APIConfig struct { | |||||||
| // AuthConfig is the configuration for the auth service | // AuthConfig is the configuration for the auth service | ||||||
| type AuthConfig struct { | type AuthConfig struct { | ||||||
| 	Users           Users | 	Users           Users | ||||||
| 	OauthWhitelist  []string | 	OauthWhitelist  string | ||||||
| 	SessionExpiry   int | 	SessionExpiry   int | ||||||
| 	Secret          string | 	Secret          string | ||||||
| 	CookieSecure    bool | 	CookieSecure    bool | ||||||
|   | |||||||
| @@ -288,7 +288,7 @@ func ParseSecretFile(contents string) string { | |||||||
| // Check if a string matches a regex or a whitelist | // Check if a string matches a regex or a whitelist | ||||||
| func CheckWhitelist(whitelist string, str string) bool { | func CheckWhitelist(whitelist string, str string) bool { | ||||||
| 	// Check if the whitelist is empty | 	// Check if the whitelist is empty | ||||||
| 	if len(whitelist) == 0 { | 	if len(strings.TrimSpace(whitelist)) == 0 { | ||||||
| 		return true | 		return true | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Stavros
					Stavros