@@ -41,7 +41,44 @@ def verify_token(token, force_refresh_jwks: false, algorithms: ["RS256"], timeou
41
41
{ keys : SDK . jwks_cache . fetch ( self , kid_not_found : options [ :invalidate ] || options [ :kid_not_found ] , force_refresh : force_refresh_jwks ) }
42
42
end
43
43
44
- JWT . decode ( token , nil , true , algorithms : algorithms , exp_leeway : timeout , jwks : jwk_loader ) . first
44
+ claims = JWT . decode ( token , nil , true , algorithms : algorithms , exp_leeway : timeout , jwks : jwk_loader ) . first
45
+
46
+ # orgs
47
+ if claims [ "v" ] . nil? || claims [ "v" ] == 1
48
+ claims [ "v" ] = 1
49
+ elsif claims [ "v" ] == 2 && claims [ "o" ]
50
+ claims [ "org_id" ] = claims [ "o" ] . fetch ( "id" , nil )
51
+ claims [ "org_slug" ] = claims [ "o" ] . fetch ( "slg" , nil )
52
+ claims [ "org_role" ] = "org:#{ claims [ "o" ] . fetch ( "rol" , nil ) } "
53
+
54
+ org_permissions = compute_org_permissions_from_v2_token ( claims )
55
+ claims [ "org_permissions" ] = org_permissions if org_permissions . any?
56
+ claims . delete ( "o" )
57
+ claims . delete ( "fea" )
58
+ end
59
+
60
+ claims
61
+ end
62
+
63
+ private
64
+
65
+ def compute_org_permissions_from_v2_token ( claims )
66
+ features = claims [ "fea" ] . split ( "," )
67
+ permissions = claims [ "o" ] [ "per" ] . split ( "," )
68
+ mappings = claims [ "o" ] [ "fpm" ] . split ( "," )
69
+ org_permissions = [ ]
70
+
71
+ mappings . each_with_index do |mapping , i |
72
+ scope , feature = features [ i ] . split ( ":" )
73
+
74
+ next if !scope . include? ( "o" ) # not an orgs-related permission
75
+
76
+ mapping . to_i . to_s ( 2 ) . reverse . each_char . each_with_index do |bit , i |
77
+ org_permissions << "org:#{ feature } :#{ permissions [ i ] } " if bit == "1"
78
+ end
79
+ end
80
+
81
+ org_permissions
45
82
end
46
83
end
47
84
end
0 commit comments