mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-10-30 05:35:44 +00:00 
			
		
		
		
	feat: implement multiple oauth providers in the frontend
This commit is contained in:
		
							
								
								
									
										1
									
								
								internal/assets/migrations/000002_oauth_name.down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								internal/assets/migrations/000002_oauth_name.down.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| ALTER TABLE "sessions" DROP COLUMN "oauth_name"; | ||||
							
								
								
									
										8
									
								
								internal/assets/migrations/000002_oauth_name.up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								internal/assets/migrations/000002_oauth_name.up.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| ALTER TABLE "sessions" ADD COLUMN "oauth_name" TEXT; | ||||
|  | ||||
| UPDATE | ||||
|     "sessions" | ||||
| SET | ||||
|     "oauth_name" = "Generic" | ||||
| WHERE | ||||
|     "oauth_name" IS NULL AND "provider" IS NOT NULL; | ||||
| @@ -151,10 +151,12 @@ func (app *BootstrapApp) Setup() error { | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		if provider.Name == "" && babysit[id] != "" { | ||||
| 			provider.Name = babysit[id] | ||||
| 		} else { | ||||
| 			provider.Name = utils.Capitalize(id) | ||||
| 		if provider.Name == "" { | ||||
| 			if name, ok := babysit[id]; ok { | ||||
| 				provider.Name = name | ||||
| 			} else { | ||||
| 				provider.Name = utils.Capitalize(id) | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		configuredProviders = append(configuredProviders, controller.Provider{ | ||||
|   | ||||
| @@ -84,6 +84,7 @@ type SessionCookie struct { | ||||
| 	Provider    string | ||||
| 	TotpPending bool | ||||
| 	OAuthGroups string | ||||
| 	OAuthName   string | ||||
| } | ||||
|  | ||||
| type UserContext struct { | ||||
| @@ -96,6 +97,7 @@ type UserContext struct { | ||||
| 	TotpPending bool | ||||
| 	OAuthGroups string | ||||
| 	TotpEnabled bool | ||||
| 	OAuthName   string | ||||
| } | ||||
|  | ||||
| // API responses and queries | ||||
|   | ||||
| @@ -19,6 +19,7 @@ type UserContextResponse struct { | ||||
| 	Provider    string `json:"provider"` | ||||
| 	OAuth       bool   `json:"oauth"` | ||||
| 	TotpPending bool   `json:"totpPending"` | ||||
| 	OAuthName   string `json:"oauthName"` | ||||
| } | ||||
|  | ||||
| type AppContextResponse struct { | ||||
| @@ -80,6 +81,7 @@ func (controller *ContextController) userContextHandler(c *gin.Context) { | ||||
| 		Provider:    context.Provider, | ||||
| 		OAuth:       context.OAuth, | ||||
| 		TotpPending: context.TotpPending, | ||||
| 		OAuthName:   context.OAuthName, | ||||
| 	} | ||||
|  | ||||
| 	if err != nil { | ||||
|   | ||||
| @@ -186,6 +186,7 @@ func (controller *OAuthController) oauthCallbackHandler(c *gin.Context) { | ||||
| 		Email:       user.Email, | ||||
| 		Provider:    req.Provider, | ||||
| 		OAuthGroups: utils.CoalesceToString(user.Groups), | ||||
| 		OAuthName:   service.GetName(), | ||||
| 	}) | ||||
|  | ||||
| 	if err != nil { | ||||
|   | ||||
| @@ -95,6 +95,7 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc { | ||||
| 				Email:       cookie.Email, | ||||
| 				Provider:    cookie.Provider, | ||||
| 				OAuthGroups: cookie.OAuthGroups, | ||||
| 				OAuthName:   cookie.OAuthName, | ||||
| 				IsLoggedIn:  true, | ||||
| 				OAuth:       true, | ||||
| 			}) | ||||
|   | ||||
| @@ -9,4 +9,5 @@ type Session struct { | ||||
| 	TOTPPending bool   `gorm:"column:totp_pending"` | ||||
| 	OAuthGroups string `gorm:"column:oauth_groups"` | ||||
| 	Expiry      int64  `gorm:"column:expiry"` | ||||
| 	OAuthName   string `gorm:"column:oauth_name"` | ||||
| } | ||||
|   | ||||
| @@ -210,6 +210,7 @@ func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.Sessio | ||||
| 		TOTPPending: data.TotpPending, | ||||
| 		OAuthGroups: data.OAuthGroups, | ||||
| 		Expiry:      time.Now().Add(time.Duration(expiry) * time.Second).Unix(), | ||||
| 		OAuthName:   data.OAuthName, | ||||
| 	} | ||||
|  | ||||
| 	err = auth.database.Create(&session).Error | ||||
| @@ -278,6 +279,7 @@ func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie, | ||||
| 		Provider:    session.Provider, | ||||
| 		TotpPending: session.TOTPPending, | ||||
| 		OAuthGroups: session.OAuthGroups, | ||||
| 		OAuthName:   session.OAuthName, | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -22,6 +22,7 @@ type GenericOAuthService struct { | ||||
| 	verifier           string | ||||
| 	insecureSkipVerify bool | ||||
| 	userinfoUrl        string | ||||
| 	name               string | ||||
| } | ||||
|  | ||||
| func NewGenericOAuthService(config config.OAuthServiceConfig) *GenericOAuthService { | ||||
| @@ -38,6 +39,7 @@ func NewGenericOAuthService(config config.OAuthServiceConfig) *GenericOAuthServi | ||||
| 		}, | ||||
| 		insecureSkipVerify: config.InsecureSkipVerify, | ||||
| 		userinfoUrl:        config.UserinfoURL, | ||||
| 		name:               config.Name, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -115,3 +117,7 @@ func (generic *GenericOAuthService) Userinfo() (config.Claims, error) { | ||||
|  | ||||
| 	return user, nil | ||||
| } | ||||
|  | ||||
| func (generic *GenericOAuthService) GetName() string { | ||||
| 	return generic.name | ||||
| } | ||||
|   | ||||
| @@ -33,6 +33,7 @@ type GithubOAuthService struct { | ||||
| 	context  context.Context | ||||
| 	token    *oauth2.Token | ||||
| 	verifier string | ||||
| 	name     string | ||||
| } | ||||
|  | ||||
| func NewGithubOAuthService(config config.OAuthServiceConfig) *GithubOAuthService { | ||||
| @@ -44,6 +45,7 @@ func NewGithubOAuthService(config config.OAuthServiceConfig) *GithubOAuthService | ||||
| 			Scopes:       GithubOAuthScopes, | ||||
| 			Endpoint:     endpoints.GitHub, | ||||
| 		}, | ||||
| 		name: config.Name, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -167,3 +169,7 @@ func (github *GithubOAuthService) Userinfo() (config.Claims, error) { | ||||
|  | ||||
| 	return user, nil | ||||
| } | ||||
|  | ||||
| func (github *GithubOAuthService) GetName() string { | ||||
| 	return github.name | ||||
| } | ||||
|   | ||||
| @@ -28,6 +28,7 @@ type GoogleOAuthService struct { | ||||
| 	context  context.Context | ||||
| 	token    *oauth2.Token | ||||
| 	verifier string | ||||
| 	name     string | ||||
| } | ||||
|  | ||||
| func NewGoogleOAuthService(config config.OAuthServiceConfig) *GoogleOAuthService { | ||||
| @@ -39,6 +40,7 @@ func NewGoogleOAuthService(config config.OAuthServiceConfig) *GoogleOAuthService | ||||
| 			Scopes:       GoogleOAuthScopes, | ||||
| 			Endpoint:     endpoints.Google, | ||||
| 		}, | ||||
| 		name: config.Name, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -111,3 +113,7 @@ func (google *GoogleOAuthService) Userinfo() (config.Claims, error) { | ||||
|  | ||||
| 	return user, nil | ||||
| } | ||||
|  | ||||
| func (google *GoogleOAuthService) GetName() string { | ||||
| 	return google.name | ||||
| } | ||||
|   | ||||
| @@ -14,6 +14,7 @@ type OAuthService interface { | ||||
| 	GetAuthURL(state string) string | ||||
| 	VerifyCode(code string) error | ||||
| 	Userinfo() (config.Claims, error) | ||||
| 	GetName() string | ||||
| } | ||||
|  | ||||
| type OAuthBrokerService struct { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Stavros
					Stavros