2024-10-28 14:58:11 +01:00
|
|
|
package claims
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"oc-auth/conf"
|
|
|
|
"oc-auth/infrastructure/perms_connectors"
|
|
|
|
"oc-auth/infrastructure/utils"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HydraClaims struct{}
|
|
|
|
|
|
|
|
func (h HydraClaims) generateKey(relation string, path string) (string, error) {
|
|
|
|
method, err := utils.ExtractMethod(relation, false)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
p := strings.ReplaceAll(strings.ToUpper(path), "/", "_")
|
|
|
|
return strings.ToLower(method.String()) + "_" + p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode key expect to extract method and path from key
|
|
|
|
func (h HydraClaims) decodeKey(key string) (tools.METHOD, string, error) {
|
|
|
|
s := strings.Split(key, "_")
|
|
|
|
if len(s) < 2 {
|
|
|
|
return tools.GET, "", errors.New("invalid key")
|
|
|
|
}
|
|
|
|
meth, err := utils.ExtractMethod(s[0], false)
|
|
|
|
if err != nil {
|
|
|
|
return meth, "", err
|
|
|
|
}
|
|
|
|
p := strings.ReplaceAll(strings.ToLower(s[1]), "_", "/")
|
|
|
|
return meth, p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h HydraClaims) DecodeSignature(host string, signature string, publicKey string) (bool, error) {
|
|
|
|
hashed := sha256.Sum256([]byte(host))
|
2024-10-30 12:38:25 +01:00
|
|
|
spkiBlock, _ := pem.Decode([]byte(publicKey)) // get public key into a variable
|
|
|
|
err := VerifyDefault(hashed[:], spkiBlock.Bytes, signature)
|
2024-10-28 14:58:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h HydraClaims) encodeSignature(host string) (string, error) {
|
|
|
|
hashed := sha256.Sum256([]byte(host))
|
|
|
|
// READ FILE TO GET PRIVATE KEY FROM PVK PEM PATH
|
2024-10-30 12:38:25 +01:00
|
|
|
content, err := os.ReadFile(conf.GetConfig().PrivateKeyPath)
|
2024-10-28 14:58:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
privateKey := string(content)
|
|
|
|
spkiBlock, _ := pem.Decode([]byte(privateKey))
|
2024-10-30 12:38:25 +01:00
|
|
|
return SignDefault(hashed[:], spkiBlock.Bytes)
|
2024-10-28 14:58:11 +01:00
|
|
|
}
|
|
|
|
|
2024-10-30 12:38:25 +01:00
|
|
|
func (h HydraClaims) DecodeClaimsInToken(host string, method string, forward string, sessionClaims Claims, publicKey string) (bool, error) {
|
|
|
|
idTokenClaims := sessionClaims.Session.IDToken
|
|
|
|
if idTokenClaims["signature"] == nil {
|
|
|
|
return false, errors.New("no signature found")
|
2024-10-28 14:58:11 +01:00
|
|
|
}
|
|
|
|
signature := idTokenClaims["signature"].(string)
|
|
|
|
if ok, err := h.DecodeSignature(host, signature, publicKey); !ok {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-10-30 12:38:25 +01:00
|
|
|
claims := sessionClaims.Session.AccessToken
|
2024-10-28 14:58:11 +01:00
|
|
|
path := strings.ReplaceAll(forward, "http://"+host, "")
|
|
|
|
splittedPath := strings.Split(path, "/")
|
|
|
|
for m, p := range claims {
|
|
|
|
splittedP := strings.Split(p.(string), "/")
|
|
|
|
if len(splittedP) != len(splittedPath) {
|
|
|
|
return false, errors.New("invalid path")
|
|
|
|
}
|
|
|
|
for i, v := range splittedP {
|
|
|
|
if strings.Contains(v, ":") { // is a param
|
|
|
|
continue
|
|
|
|
} else if v != splittedPath[i] {
|
|
|
|
meth, _, err := h.decodeKey(m)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
perm := perms_connectors.Permission{
|
|
|
|
Relation: "permits" + strings.ToLower(meth.String()),
|
|
|
|
Object: p.(string),
|
|
|
|
}
|
|
|
|
return perms_connectors.GetPermissionConnector().CheckPermission(perm, nil, true), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, errors.New("no permission found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// add claims to token method of HydraTokenizer
|
|
|
|
func (h HydraClaims) AddClaimsToToken(userId string, host string) Claims {
|
|
|
|
claims := Claims{}
|
2024-10-30 16:18:21 +01:00
|
|
|
perms, err := perms_connectors.KetoConnector{}.GetPermissionByUser(userId, false)
|
2024-10-28 14:58:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return claims
|
|
|
|
}
|
2024-10-30 12:38:25 +01:00
|
|
|
claims.Session.AccessToken = make(map[string]interface{})
|
|
|
|
claims.Session.IDToken = make(map[string]interface{})
|
2024-10-28 14:58:11 +01:00
|
|
|
for _, perm := range perms {
|
|
|
|
key, err := h.generateKey(perm.Relation, perm.Object)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
claims.Session.AccessToken[key] = perm.Object
|
|
|
|
}
|
|
|
|
sign, err := h.encodeSignature(host)
|
|
|
|
if err != nil {
|
|
|
|
return claims
|
|
|
|
}
|
|
|
|
claims.Session.IDToken["signature"] = sign
|
|
|
|
return claims
|
|
|
|
}
|
|
|
|
|
|
|
|
// add signature in the token MISSING
|