Skip to content

Commit a29d9eb

Browse files
committed
feat: Keycloak role permissions system
1 parent 6587754 commit a29d9eb

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

app/models/oauth2_token.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
package models
22

3+
import (
4+
"database/sql/driver"
5+
"encoding/json"
6+
"errors"
7+
)
8+
39
type Oauth2Token struct {
4-
UserID string `json:"user_id"`
5-
TokenType string `json:"token_type"`
6-
AccessToken string `json:"access_token"`
7-
RefreshToken string `json:"refresh_token"`
8-
ExpiresIn int `json:"expires_in"`
9-
RefreshExpiresIn int `json:"refresh_expires_in"`
10+
UserID string `json:"user_id"`
11+
TokenType string `json:"token_type"`
12+
AccessToken string `json:"access_token"`
13+
RefreshToken string `json:"refresh_token"`
14+
ExpiresIn int `json:"expires_in"`
15+
RefreshExpiresIn int `json:"refresh_expires_in"`
16+
Permissions StrArray `json:"permissions" gorm:"type:jsonb;index,type:gin"`
1017
}
1118

1219
func (Oauth2Token) TableName() string {
1320
return "oauth2_tokens"
1421
}
22+
23+
type StrArray []string
24+
25+
// Value Marshal
26+
func (a StrArray) Value() (driver.Value, error) {
27+
return json.Marshal(a)
28+
}
29+
30+
// Scan Unmarshal
31+
func (a *StrArray) Scan(value interface{}) error {
32+
b, ok := value.([]byte)
33+
if !ok {
34+
return errors.New("type assertion to []byte failed")
35+
}
36+
return json.Unmarshal(b, &a)
37+
}

internal/auth/keycloak.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,17 @@ func RefreshTokenIfNecessary(user_id string) error {
4747
keycloak.Ctx = context.Background()
4848
}
4949

50-
rptResult, err := keycloak.Client.RetrospectToken(
50+
result, _, err := keycloak.Client.DecodeAccessToken(
5151
keycloak.Ctx,
52-
token.RefreshToken,
53-
helpers.Env("KEYCLOAK_CLIENT_ID", ""),
54-
helpers.Env("KEYCLOAK_CLIENT_SECRET", ""),
52+
token.AccessToken,
5553
helpers.Env("KEYCLOAK_REALM", ""),
5654
)
5755

5856
if err != nil {
59-
return errors.New("an error occured while retrospecting token")
57+
return errors.New("an error occured while validating token")
6058
}
6159

62-
if !*rptResult.Active {
60+
if !result.Valid {
6361
err := RefreshToken(token)
6462
if err != nil {
6563
return err

internal/liman/role_system.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ func GetPermissions(user *models.User, extFilter string) ([]string, map[string]s
3030
variables = helpers.MergeStringMaps(variables, variable)
3131
}
3232

33+
if user.AuthType == "keycloak" {
34+
token := &models.Oauth2Token{}
35+
database.Connection().First(&token, "user_id = ?", user.ID)
36+
37+
if token.UserID != "" {
38+
permissions = append(permissions, token.Permissions...)
39+
}
40+
}
41+
3342
return permissions, variables, nil
3443
}
3544

0 commit comments

Comments
 (0)