Skip to content

Commit

Permalink
JWT generation fixes (#61)
Browse files Browse the repository at this point in the history
* config: add a separate [signing] section

* rpc: update the config use in JWT generation

* etc: configure JWT signing for all envs

* rpc: fix failing test
  • Loading branch information
patrislav authored Jul 29, 2024
1 parent c71f555 commit f82a71b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type Config struct {
Mode Mode `toml:"-"`
Region string `toml:"region"`
Service ServiceConfig `toml:"service"`
BaseURL string `toml:"base_url"`
Admin AdminConfig `toml:"admin"`
Endpoints EndpointsConfig `toml:"endpoints"`
KMS KMSConfig `toml:"kms"`
SES SESConfig `toml:"ses"`
Builder BuilderConfig `toml:"builder"`
Database DatabaseConfig `toml:"database"`
Signing SigningConfig `toml:"signing"`
Telemetry telemetry.Config `toml:"telemetry"`
Tracing TracingConfig `toml:"tracing"`
}
Expand Down Expand Up @@ -67,6 +67,11 @@ type BuilderConfig struct {
SecretID string `toml:"secret_id"`
}

type SigningConfig struct {
Issuer string `toml:"issuer"`
AudiencePrefix string `toml:"audience_prefix"`
}

type TracingConfig struct {
Endpoint string `toml:"endpoint"`
}
Expand Down
4 changes: 4 additions & 0 deletions etc/waas-auth.dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ QwIDAQAB
[builder]
base_url = "https://dev-api.sequence.build"
secret_id = "dev-builder-jwt"

[signing]
issuer = "https://dev-waas.sequence.app"
audience_prefix = "https://dev.sequence.build/project/"
4 changes: 4 additions & 0 deletions etc/waas-auth.next.conf
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ QwIDAQAB
[builder]
base_url = "https://next-api.sequence.build"
secret_id = "next-builder-jwt"

[signing]
issuer = "https://next-waas.sequence.app"
audience_prefix = "https://next.sequence.build/project/"
4 changes: 4 additions & 0 deletions etc/waas-auth.prod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ MQIDAQAB
[builder]
base_url = "https://api.sequence.build"
secret_id = "prod-builder-jwt"

[signing]
issuer = "https://waas.sequence.app"
audience_prefix = "https://sequence.build/project/"
5 changes: 4 additions & 1 deletion etc/waas-auth.sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ region = "us-east-1"
enclave_port = 9123
proxy_port = 9124
debug_profiler = true
base_url = "http://localhost:9123"

[telemetry]
allow_any = true
Expand Down Expand Up @@ -52,3 +51,7 @@ QwIDAQAB
[builder]
base_url = "http://host.docker.internal:9999"
secret_id = "BuilderJWT"

[signing]
issuer = "http://localhost:9123"
audience_prefix = "http://host.docker.internal:9999/project/"
10 changes: 5 additions & 5 deletions rpc/identity_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"

"github.com/0xsequence/go-sequence/intents"
Expand Down Expand Up @@ -38,7 +39,7 @@ type jwks struct {

func (s *RPC) handleOpenidConfiguration(w http.ResponseWriter, r *http.Request) {
cfg := &openidConfig{
JWKSURI: s.Config.BaseURL + "/.well-known/jwks.json",
JWKSURI: s.Config.Signing.Issuer + "/.well-known/jwks.json",
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -100,18 +101,17 @@ func (s *RPC) getIDToken(
return nil, fmt.Errorf("getting wallet address: %w", err)
}

aud := fmt.Sprintf("%s/project/%d", s.Config.Builder.BaseURL, tnt.ProjectID)
iat := time.Now()
exp := iat.Add(10 * time.Minute)

tokenBuilder := jwt.NewBuilder().
Subject(walletAddr).
Audience([]string{aud}).
Issuer(s.Config.BaseURL).
Audience([]string{s.Config.Signing.AudiencePrefix + strconv.Itoa(int(tnt.ProjectID))}).
Issuer(s.Config.Signing.Issuer).
IssuedAt(iat).
Expiration(exp).
Claim("auth_time", sessData.CreatedAt.Unix()).
Claim(s.Config.BaseURL+"/identity", identity)
Claim(s.Config.Signing.Issuer+"/identity", identity)

if account.Email != "" {
tokenBuilder.Claim("email", account.Email)
Expand Down
4 changes: 2 additions & 2 deletions rpc/identity_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestRPC_SendIntent_GetIdToken(t *testing.T) {

srv := httptest.NewServer(svc.Handler())
defer srv.Close()
svc.Config.BaseURL = srv.URL
svc.Config.Builder.BaseURL = "https://sequence.build"
svc.Config.Signing.Issuer = srv.URL
svc.Config.Signing.AudiencePrefix = "https://sequence.build/project/"

intentData := &intents.IntentDataGetIdToken{
Wallet: walletAddr,
Expand Down

0 comments on commit f82a71b

Please sign in to comment.