Skip to content

Commit b376ee3

Browse files
authored
feat: use multi roles login (#60)
* feat: use multi roles login - add support for protected roles - refactor oauth code * fix: adminUpdate role validation * fix: update app
1 parent 27944cf commit b376ee3

23 files changed

+248
-219
lines changed

.env.sample

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ADMIN_SECRET=admin
55
DISABLE_EMAIL_VERIFICATION=true
66
JWT_SECRET=random_string
77
JWT_TYPE=HS256
8-
ROLES=user,admin
9-
DEFAULT_ROLE=user
8+
ROLES=user
9+
DEFAULT_ROLES=user
10+
PROTECTED_ROLES=admin
1011
JWT_ROLE_CLAIM=role

app/build/bundle.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"author": "Lakhan Samani",
1111
"license": "ISC",
1212
"dependencies": {
13-
"@authorizerdev/authorizer-react": "^0.1.0-beta.18",
13+
"@authorizerdev/authorizer-react": "^0.1.0-beta.19",
1414
"@types/react": "^17.0.15",
1515
"@types/react-dom": "^17.0.9",
1616
"esbuild": "^0.12.17",

server/constants/constants.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ var (
2323
DISABLE_BASIC_AUTHENTICATION = "false"
2424

2525
// ROLES
26-
ROLES = []string{}
27-
DEFAULT_ROLE = ""
28-
JWT_ROLE_CLAIM = "role"
26+
ROLES = []string{}
27+
PROTECTED_ROLES = []string{}
28+
DEFAULT_ROLES = []string{}
29+
JWT_ROLE_CLAIM = "role"
2930

3031
// OAuth login
3132
GOOGLE_CLIENT_ID = ""

server/env.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/authorizerdev/authorizer/server/constants"
10+
"github.com/authorizerdev/authorizer/server/utils"
1011
"github.com/joho/godotenv"
1112
)
1213

@@ -63,7 +64,6 @@ func InitEnv() {
6364
constants.RESET_PASSWORD_URL = strings.TrimPrefix(os.Getenv("RESET_PASSWORD_URL"), "/")
6465
constants.DISABLE_BASIC_AUTHENTICATION = os.Getenv("DISABLE_BASIC_AUTHENTICATION")
6566
constants.DISABLE_EMAIL_VERIFICATION = os.Getenv("DISABLE_EMAIL_VERIFICATION")
66-
constants.DEFAULT_ROLE = os.Getenv("DEFAULT_ROLE")
6767
constants.JWT_ROLE_CLAIM = os.Getenv("JWT_ROLE_CLAIM")
6868

6969
if constants.ADMIN_SECRET == "" {
@@ -136,28 +136,45 @@ func InitEnv() {
136136

137137
rolesSplit := strings.Split(os.Getenv("ROLES"), ",")
138138
roles := []string{}
139-
defaultRole := ""
139+
if len(rolesSplit) == 0 {
140+
roles = []string{"user"}
141+
}
142+
143+
defaultRoleSplit := strings.Split(os.Getenv("DEFAULT_ROLES"), ",")
144+
defaultRoles := []string{}
145+
146+
if len(defaultRoleSplit) == 0 {
147+
defaultRoles = []string{"user"}
148+
}
149+
150+
protectedRolesSplit := strings.Split(os.Getenv("PROTECTED_ROLES"), ",")
151+
protectedRoles := []string{}
152+
153+
if len(protectedRolesSplit) > 0 {
154+
for _, val := range protectedRolesSplit {
155+
trimVal := strings.TrimSpace(val)
156+
protectedRoles = append(protectedRoles, trimVal)
157+
}
158+
}
140159

141160
for _, val := range rolesSplit {
142161
trimVal := strings.TrimSpace(val)
143162
if trimVal != "" {
144163
roles = append(roles, trimVal)
145164
}
146165

147-
if trimVal == constants.DEFAULT_ROLE {
148-
defaultRole = trimVal
166+
if utils.StringContains(defaultRoleSplit, trimVal) {
167+
defaultRoles = append(defaultRoles, trimVal)
149168
}
150169
}
151-
if len(roles) > 0 && defaultRole == "" {
152-
panic(`Invalid DEFAULT_ROLE environment variable. It can be one from give ROLES environment variable value`)
153-
}
154170

155-
if len(roles) == 0 {
156-
roles = []string{"user", "admin"}
157-
constants.DEFAULT_ROLE = "user"
171+
if len(roles) > 0 && len(defaultRoles) == 0 && len(defaultRoleSplit) > 0 {
172+
panic(`Invalid DEFAULT_ROLE environment variable. It can be one from give ROLES environment variable value`)
158173
}
159174

160175
constants.ROLES = roles
176+
constants.DEFAULT_ROLES = defaultRoles
177+
constants.PROTECTED_ROLES = protectedRoles
161178

162179
if constants.JWT_ROLE_CLAIM == "" {
163180
constants.JWT_ROLE_CLAIM = "role"

server/graph/generated/generated.go

Lines changed: 59 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/graph/model/models_gen.go

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)