danieleteti/delphimvcframework

Make all JWT custom claims data available in the context

landrix opened this issue · 1 comments

Make all JWT custom claims data available in the context

Like:

Context.LoggedUser.UserName := lJWT.CustomClaims['username'];
Context.LoggedUser.Roles.AddRange(lJWT.CustomClaims['roles'].Split([',']));
Context.LoggedUser.LoggedSince := lJWT.Claims.IssuedAt;

So I do not need to unpack the token a second time in the controller procedure.

Fixed in 2.1.6 (carbon)

Since 2.1.6 (carbon) Context.LoggedUser.CustomData is filled with all the JWT CustomClaims. You can also add CustomClaims inside the OnAuthentication method using the TMVCCustomData (former TSessionData) instance as shown below.

procedure TMyAppAuth.OnAuthentication(const UserName, Password: string;
  UserRoles: TList<System.string>;
  var IsValid: Boolean; 
  const CustomData: TMVCCustomData);
var
  LUsersService: IUsersService;
  LDS: TDataSet;
begin
  LUsersService := Container.Resolve<IUsersService>;
  IsValid := LUsersService.IsValidUser(UserName, Password);
  if IsValid then
  begin
    LDS := LUsersService.GetUserInfoByUserName(UserName);
    try
      CustomData.Add('customer_guid', LDS.FieldByName('guid').AsString);
      CustomData.Add('customer_email', LDS.FieldByName('email').AsString);
    finally
      LDS.Free;
    end;
  end;
end;