-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom claims for setting grpc/server addr in token (#150)
- Loading branch information
1 parent
7bdfa78
commit 73adb77
Showing
22 changed files
with
247 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package loader | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type addresses struct { | ||
serverURL string | ||
grpcBroadcastAddress string | ||
} | ||
|
||
func getAddressesFromJWT(token string) (*addresses, error) { | ||
claims, err := extractClaimsFromJWT(token) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
serverURL, ok := claims["server_url"].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("server_url claim not found") | ||
} | ||
|
||
grpcBroadcastAddress, ok := claims["grpc_broadcast_address"].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("grpc_broadcast_address claim not found") | ||
} | ||
|
||
return &addresses{ | ||
serverURL: serverURL, | ||
grpcBroadcastAddress: grpcBroadcastAddress, | ||
}, nil | ||
} | ||
|
||
func extractClaimsFromJWT(token string) (map[string]interface{}, error) { | ||
parts := strings.Split(token, ".") | ||
if len(parts) != 3 { | ||
return nil, fmt.Errorf("invalid token format") | ||
} | ||
|
||
claimsData, err := base64.RawURLEncoding.DecodeString(parts[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var claims map[string]interface{} | ||
err = json.Unmarshal(claimsData, &claims) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return claims, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package loader | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExtractClaimsFromJWT(t *testing.T) { | ||
token := "eyJhbGciOiJFUzI1NiIsICJraWQiOiJRMzNPaGcifQ.eyJhdWQiOiJodHRwczovL2FwcC5kZXYuaGF0Y2hldC10b29scy5jb20iLCAiZXhwIjoxNzE0ODc4NDEyLCAiZ3JwY19icm9hZGNhc3RfYWRkcmVzcyI6IjEyNy4wLjAuMTo3MDcwIiwgImlhdCI6MTcwNzEwMjQxMiwgImlzcyI6Imh0dHBzOi8vYXBwLmRldi5oYXRjaGV0LXRvb2xzLmNvbSIsICJzZXJ2ZXJfdXJsIjoiaHR0cHM6Ly9hcHAuZGV2LmhhdGNoZXQtdG9vbHMuY29tIiwgInN1YiI6IjcwN2QwODU1LTgwYWItNGUxZi1hMTU2LWYxYzQ1NDZjYmY1MiIsICJ0b2tlbl9pZCI6IjI1NzFkODMwLWFmNDgtNDYyZS1hNDFlLTRlZWJkMjUwN2I0NyJ9.abcdefg" // #nosec G101 | ||
|
||
claims, err := extractClaimsFromJWT(token) | ||
|
||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, claims["server_url"], "https://app.dev.hatchet-tools.com") | ||
assert.Equal(t, claims["grpc_broadcast_address"], "127.0.0.1:7070") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import base64 | ||
import json | ||
|
||
def get_addresses_from_jwt(token: str) -> (str, str): | ||
claims = extract_claims_from_jwt(token) | ||
|
||
return claims.get('server_url'), claims.get('grpc_broadcast_address') | ||
|
||
def extract_claims_from_jwt(token: str): | ||
parts = token.split('.') | ||
if len(parts) != 3: | ||
raise ValueError('Invalid token format') | ||
|
||
claims_part = parts[1] | ||
claims_part += '=' * ((4 - len(claims_part) % 4) % 4) # Padding for base64 decoding | ||
claims_data = base64.urlsafe_b64decode(claims_part) | ||
claims = json.loads(claims_data) | ||
|
||
return claims |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "hatchet-sdk" | ||
version = "0.8.0" | ||
version = "0.9.0" | ||
description = "" | ||
authors = ["Alexander Belanger <[email protected]>"] | ||
readme = "README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.