@@ -17,6 +17,7 @@ import (
1717 "github.com/getfider/fider/app/pkg/bus"
1818
1919 "github.com/getfider/fider/app"
20+ "github.com/getfider/fider/app/pkg/env"
2021 "github.com/getfider/fider/app/pkg/errors"
2122 "github.com/getfider/fider/app/pkg/jwt"
2223 "github.com/getfider/fider/app/pkg/log"
@@ -91,6 +92,17 @@ func OAuthToken() web.HandlerFunc {
9192 return c .Failure (err )
9293 }
9394
95+ // Check if user has required roles (if OAUTH_ALLOWED_ROLES is configured)
96+ if ! hasAllowedRole (oauthUser .Result .Roles ) {
97+ log .Warnf (c , "User @{UserID} attempted OAuth login but does not have required role. User roles: @{UserRoles}, Allowed roles: @{AllowedRoles}" ,
98+ dto.Props {
99+ "UserID" : oauthUser .Result .ID ,
100+ "UserRoles" : oauthUser .Result .Roles ,
101+ "AllowedRoles" : env .Config .OAuth .AllowedRoles ,
102+ })
103+ return c .Redirect ("/access-denied" )
104+ }
105+
94106 var user * entity.User
95107
96108 userByProvider := & query.GetUserByProvider {Provider : provider , UID : oauthUser .Result .ID }
@@ -264,3 +276,42 @@ func SignInByOAuth() web.HandlerFunc {
264276 return c .Redirect (authURL .Result )
265277 }
266278}
279+
280+ // hasAllowedRole checks if the user has any of the allowed roles configured in OAUTH_ALLOWED_ROLES
281+ // If OAUTH_ALLOWED_ROLES is not set or empty, all users are allowed (returns true)
282+ // If set, user must have at least one of the specified roles
283+ func hasAllowedRole (userRoles []string ) bool {
284+ allowedRolesConfig := strings .TrimSpace (env .Config .OAuth .AllowedRoles )
285+
286+ // If no roles restriction is configured, allow all users
287+ if allowedRolesConfig == "" {
288+ return true
289+ }
290+
291+ // Parse allowed roles from config (semicolon-separated)
292+ allowedRoles := strings .Split (allowedRolesConfig , ";" )
293+ allowedRolesMap := make (map [string ]bool )
294+ for _ , role := range allowedRoles {
295+ role = strings .TrimSpace (role )
296+ if role != "" {
297+ allowedRolesMap [role ] = true
298+ }
299+ }
300+
301+ // If no valid roles in config, allow all
302+ if len (allowedRolesMap ) == 0 {
303+ return true
304+ }
305+
306+ // Check if user has any of the allowed roles
307+ for _ , userRole := range userRoles {
308+ userRole = strings .TrimSpace (userRole )
309+ if allowedRolesMap [userRole ] {
310+ return true
311+ }
312+ }
313+
314+ // User doesn't have any of the required roles
315+ return false
316+ }
317+
0 commit comments