#1 2025-10-05 10:59:56

anouri
Member
Registered: 2024-02-11
Posts: 116

How can I get secret from TJwtHS256?

Hi AB,
First of all, thank you for the great framework.
I am using the following code for JWT implementation:
https://gist.github.com/a-nouri/c61aef8 … f413b4538f
The code works, and I can retrieve the JWT JSON using the Delphi REST debugger (at http://localhost:8282/root/Auth?UserName=User&Password=synopse):

{
    "result": "302102+5B8F237C0D720FD66C3230C054AB161362E8E0FC12136AC3AE8196765B6C2779",
    "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9ua2V5IjoiMzAyMTAyKzVCOEYyMzdDMEQ3MjBGRDY2QzMyMzBDMDU0QUIxNjEzNjJFOEUwRkMxMjEzNkFDM0FFODE5Njc2NUI2QzI3NzkiLCJpc3MiOiJVc2VyIiwic3ViIjoiand0LmFjY2VzcyIsImV4cCI6MTc1OTY2MjE1MX0.iUQmvtUov2YvB02UnJQkq0RXZNzN0AJzAcUCC4E5kBo"
}

However, when I try to validate it on jwt.io, it requires the secret key.
I can't find a way to retrieve the secret, but when debugging the Create method, I can see this value:

893ee7b5d45733a3bb3f8907a46233a3a510f05533de8d50ac10edbc7653f2f8

Thanks for your help!

Last edited by anouri (2025-10-05 11:11:52)

Offline

#2 2025-10-05 16:12:17

flydev
Member
From: France
Registered: 2020-11-27
Posts: 131
Website

Re: How can I get secret from TJwtHS256?

The secret is the same as the param you set on TJwtHS256.Create if SecretPbkdf2Round is 0. If you didn't modified the public example then a new secret is generated on each server start and it's the sha256 hash of a random guid, see there.

Just set a breakpoint and copy the value, or echo the key on the console (or generate a new hash from a dummy secret) and then copy paste the key on jwt.io, you should get a valid signature.

Last edited by flydev (2025-10-05 16:15:20)

Offline

#3 2025-10-06 11:37:06

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

Hi flydev.
there were a minor bug in your code and I fixed it.
changed function is this:
//function TRestServerAuthenticationJWT.Auth(Ctxt: TRestServerURIContext; const aUserName: RawUTF8): boolean; original
function TRestServerAuthenticationJWT.Auth(Ctxt: TRestServerURIContext): boolean; //chenged

I posted link in gist in my first post of topic.

if I underestand correctly "Create(SHA256(GUIDToRawUTF8(vGUID))" is secret. right?

JWTForUnauthenticatedRequest :=
    TJwtHS256.
      Create(SHA256(GUIDToRawUTF8(vGUID)), 0, [jrcIssuer, jrcSubject], [],
        JWTDefaultTimeout);

Last edited by anouri (2025-10-06 11:40:22)

Offline

#4 2025-10-06 11:44:45

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

Yes. I got it. and now I can find secret.
many thanks!

Offline

#5 2025-10-06 11:48:38

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

I have another question.
Is it possible to create interface based service for authentication and use it in client side?
something like this:

type
  IAuthentication  = interface(IInvokable)
    ['{C92DCBEA-C680-40BD-8D9C-3E6F2ED9C9CF}']
    procedure GetToken...;
    procedure RefershToken...;
    procedure IsTokenValid...;
    ...

  end;

Last edited by anouri (2025-10-06 11:51:58)

Offline

#6 2025-10-06 12:42:05

flydev
Member
From: France
Registered: 2020-11-27
Posts: 131
Website

Re: How can I get secret from TJwtHS256?

Yes I've read your comment (I edited it on the PR smile )  It was not really a bug but a change on the framework implementation. I didn't had the time to push my changes and merging @koraycayiroglu code.

Is it possible to create interface based service for authentication and use it in client side?

Yes you can, just register your service with bypassauthentication true. On the initial sample they were implemented as service methods as I think it fit better to access service context. You can mix both interface/service methods without problem.

Offline

#7 2025-10-06 13:30:20

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

Thank you. I will try

Offline

#8 2025-10-06 14:39:06

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

I saw in other rest service swagger, there are authentication button in the top of page. but for your sample project this button not exists!
How can I enable this option?

Offline

#9 2025-10-06 16:28:33

flydev
Member
From: France
Registered: 2020-11-27
Posts: 131
Website

Re: How can I get secret from TJwtHS256?

I dont understand this one sorry - if you mean swagger editor, you will get a button if you define some security schemes.  Which api definition are  you talking about ?

Last edited by flydev (2025-10-06 16:28:49)

Offline

#10 Yesterday 05:27:30

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

I copy some template files to my bin folder and then use swagger this way:

https://petstore.swagger.io/?url=http://localhost:888/root/wrapper/Swagger/mORMotClient.json.txt

Last edited by anouri (Yesterday 05:30:38)

Offline

#11 Yesterday 06:37:21

flydev
Member
From: France
Registered: 2020-11-27
Posts: 131
Website

Re: How can I get secret from TJwtHS256?

I see, your file is probably missing something or contain a bad formatting. Swagger doesn't care about the server itself, it's about the file definition.

Offline

#12 Yesterday 08:31:34

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

I copied "Swagger.json.mustache" file form somewhere into tempates folder and I don't familiar with that sad

Last edited by anouri (Yesterday 08:31:52)

Offline

#13 Yesterday 09:13:30

flydev
Member
From: France
Registered: 2020-11-27
Posts: 131
Website

Re: How can I get secret from TJwtHS256?

I can help your but put some efforts smile
just share the file

Offline

#14 Yesterday 09:17:23

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

Offline

#15 Yesterday 11:35:49

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

I have one problem:
I created jwt rest server and mormot create users and groups in sqlite database.
but I have my own users table in my mysql database too. how can I use my own users and groups tables in my mysql database instead of mormot sqlite database?

Last edited by anouri (Yesterday 11:36:58)

Offline

#16 Yesterday 12:06:47

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

I find this :
OrmMapExternalAll(MyModel, aDbConnection, []);

and now mormot create jwtauthuser and authgroup tables in mysql database.

Offline

#17 Today 07:31:36

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

After calling OrmMapExternalAll I add user to my table in jwtauthuser table, but mormot does not add this to internal storage in memory, and when I call Auth URI, I got an error:

{
    "errorCode": 403,
    "errorText": "Authentication Failed: Unknown user (2)"
}

how can I reload settings? Suppose when service is running I add user to table. How can I retereive users list again?

Last edited by anouri (Today 07:33:30)

Offline

#18 Today 08:56:27

flydev
Member
From: France
Registered: 2020-11-27
Posts: 131
Website

Re: How can I get secret from TJwtHS256?

This is beyond the scope of jwt thing, you didn't described how you are adding user and I then assume you are adding manually an user into the external auth user table: if ORM cache is enabled, then this behavior is expected. You could read that you are hitting orm cache on the console by enabling server logs (and by reading the manual).

Thats said, set db.usecache to false or write a small rest client tool for adding your users..

Last edited by flydev (Today 08:56:43)

Offline

#19 Today 09:09:40

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

I add users to my table in database manager tools (like heidisql, ...).
After many try and error I find that when I delcare:

  TMyRest = class(TRestServerDB)

It works. but when I use :

  TMyRest = class(TRestServerFullMemory)

it does not work!

Last edited by anouri (Today 09:11:10)

Offline

#20 Today 09:18:02

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

Is this expected bahaiviour when I use:

TMykRest = class(TRestServerFullMemory)

mormot does not retreive users from db or this is bug?
https://synopse.info/forum/profile.php?id=2

Last edited by anouri (Today 09:21:59)

Offline

#21 Today 09:35:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,226
Website

Re: How can I get secret from TJwtHS256?

TRestServerFullMemory = full memory db = no sql db by design.

You need to either
1) provide a TRestOrmServerFullMemory.FileName for local user/group storage (local JSON storage may be simple and safe enough) - optionally with TRestOrmServerFullMemory.UpdateToFile on updates, if writing at shutdown is not enough
2) switch to a TRestServerDB and an external SQL mapping - but ensure you don't publish the tables as REST
3) setup a TRestServer.OnAuthenticationUserRetrieve callback to retrieve some user information e.g. with direct SQL over an existing database

Offline

#22 Today 10:14:00

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

Thank you.

Offline

#23 Today 11:21:22

anouri
Member
Registered: 2024-02-11
Posts: 116

Re: How can I get secret from TJwtHS256?

I am using method 2. but I don't undrestand "don't publish the tables as REST"

Offline

Board footer

Powered by FluxBB