Skip to content

Commit

Permalink
Refactor id_token logic for OpenID compatibility
Browse files Browse the repository at this point in the history
Replaced `user_id` with `username` for `sub` claim in ID token to align with OpenID standards. Removed unnecessary `name` and `email` properties from `Owner` struct for streamlined data handling. Adjusted token expiration to use `access_ttl` from configuration for consistency. This update improves ID token generation and enhances interoperability with OpenID compliant systems.
  • Loading branch information
eliasjpr committed Oct 18, 2024
1 parent e668eb1 commit 0832c2c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion spec/authly_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe Authly do
id_token_decoded = Authly.jwt_decode(id_token).first

token.should be_a Authly::AccessToken
id_token_decoded["user_id"].should eq "username"
id_token_decoded["sub"].should eq "username"
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion src/authly/grant.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ module Authly

private def generate_id_token
if scope.includes? "openid"
payload = Authly.owners.id_token(auth_code["user_id"].as_s)
user_id = auth_code["user_id"].as_s
payload = Authly.owners.id_token(user_id)
payload["iss"] = Authly.config.issuer
payload["aud"] = @client_id
Authly.jwt_encode(payload)
Expand Down
9 changes: 3 additions & 6 deletions src/authly/owner.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Authly
struct Owner
property id : String = Random::Secure.hex(16)
property name : String = ""
property email : String = ""
property username : String
property password : String

Expand All @@ -29,13 +27,12 @@ module Authly
end

def id_token(user_id : String) : Hash(String, String | Int64)
user = find! { |owner| owner.id == user_id }
user = find! { |owner| owner.username == user_id }
{
"sub" => user_id,
"iat" => Time.utc.to_unix,
"exp" => 1.hour.from_now.to_unix,
"name" => user.name,
"email" => user.email,
"exp" => Authly.config.access_ttl.from_now.to_unix,
"iss" => Authly.config.issuer,
}
end

Expand Down

0 comments on commit 0832c2c

Please sign in to comment.