@@ -7,6 +7,7 @@ defmodule Console.Deployments.Settings do
77 alias Console.Services.Users
88 alias Console.Deployments . { Clusters , Services }
99 alias Console.Schema . { DeploymentSettings , User , Project , BootstrapToken , CloudConnection , FederatedCredential }
10+ require Logger
1011
1112 @ agent_vsn File . read! ( "AGENT_VERSION" ) |> String . trim ( )
1213 @ kube_vsn File . read! ( "KUBE_VERSION" ) |> String . trim ( )
@@ -327,13 +328,14 @@ defmodule Console.Deployments.Settings do
327328 """
328329 @ spec exchange_token ( binary , binary ) :: { :ok , binary } | Console . error ( )
329330 def exchange_token ( token , email ) do
330- user = Users . get_user_by_email !( email )
331+ user = Users . cached_user_by_email !( email )
331332
332- with { :token , { :ok , % { "iss" => issuer } } } <- { :token , Joken . peek_claims ( token ) } ,
333+ with { :token , { :ok , % { "iss" => issuer , "aud" => aud } } } <- { :token , Joken . peek_claims ( token ) } ,
333334 { :config , { :ok , { conf , jwks } } } <- { :config , issuer_configuration ( issuer ) } ,
334335 opts = % { client_jwks: JOSE.JWK . generate_key ( 16 ) } ,
335336 ctx = Oidcc.ClientContext . from_manual ( conf , jwks , "dummy_id" , "dummy_secret" , opts ) ,
336- opts = % { signing_algs: ctx . provider_configuration . id_token_signing_alg_values_supported } ,
337+ opts = % { signing_algs: ctx . provider_configuration . id_token_signing_alg_values_supported ,
338+ trusted_audiences: [ aud ] } ,
337339 { :validate , { :ok , claims } } <- { :validate , Oidcc.Token . validate_jwt ( token , ctx , opts ) } do
338340 FederatedCredential . for_issuer ( issuer )
339341 |> FederatedCredential . for_user ( user . id )
@@ -344,12 +346,16 @@ defmodule Console.Deployments.Settings do
344346 scopes = Enum . flat_map ( credentials , & & 1 . scopes || [ ] )
345347 |> Enum . uniq ( )
346348 sign_token ( user , scopes )
347- _ -> { :error , "no federated credential for #{ email } match jwt claims" }
349+ _ -> { :error , "no federated credentials for #{ email } match jwt claims" }
348350 end
349351 else
350352 { :token , _ } -> { :error , "invalid jwt format" }
351- { :config , _ } -> { :error , "invalid issuer url from jwt" }
352- { :validate , _ } -> { :error , "could not validate jwt" }
353+ { :config , _ } = res ->
354+ Logger . error ( "failed to fetch issuer configuration: #{ inspect ( res ) } " )
355+ { :error , "invalid issuer url from jwt" }
356+ { :validate , _ } = res ->
357+ Logger . error ( "failed to validate jwt: #{ inspect ( res ) } " )
358+ { :error , "could not validate jwt" }
353359 end
354360 end
355361
@@ -360,12 +366,22 @@ defmodule Console.Deployments.Settings do
360366 end
361367 end
362368
369+ @ quirks % {
370+ quirks: % {
371+ allow_issuer_mismatch: true ,
372+ document_overrides: % {
373+ # needed for atypical oidc implementations like Github's
374+ "authorization_endpoint" => "https://example.com/ignore/oauth/authorize" ,
375+ }
376+ }
377+ }
378+
363379 @ doc """
364380 Fetches the issuer configuration from the issuer url
365381 """
366382 @ decorate cacheable ( cache: @ cache_adapter , key: { :issuer_configuration , issuer } , opts: [ ttl: :timer . minutes ( 60 ) ] )
367383 def issuer_configuration ( issuer ) do
368- with { :ok , { conf , _ } } <- Oidcc.ProviderConfiguration . load_configuration ( issuer , % { quirks: % { allow_issuer_mismatch: true } } ) ,
384+ with { :ok , { conf , _ } } <- Oidcc.ProviderConfiguration . load_configuration ( issuer , @ quirks ) ,
369385 { :ok , { jwks , _ } } <- Oidcc.ProviderConfiguration . load_jwks ( conf . jwks_uri ) ,
370386 do: { :ok , { conf , jwks } }
371387 end
0 commit comments