magic.lambda.auth 15.8.2

.NET 6.0
dotnet add package magic.lambda.auth --version 15.8.2
NuGet\Install-Package magic.lambda.auth -Version 15.8.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="magic.lambda.auth" Version="15.8.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add magic.lambda.auth --version 15.8.2
#r "nuget: magic.lambda.auth, 15.8.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install magic.lambda.auth as a Cake Addin
#addin nuget:?package=magic.lambda.auth&version=15.8.2

// Install magic.lambda.auth as a Cake Tool
#tool nuget:?package=magic.lambda.auth&version=15.8.2

magic.lambda.auth - How to do authentication and authorisation from Hyperlambda

The magic.lambda.auth project contains authentication and authorization helper slots for Magic. The project allows you to create and consume JWT tokens, to secure your Magic cloudlet. The project contains the following slots.

  • [auth.ticket.get] - Returns the JWT token's payload as a lambda structure, and also verifies the token in the process
  • [auth.ticket.create] - Creates a new JWT token, that you can return to your client, any ways you see fit
  • [auth.ticket.refresh] - Refreshes a JWT token. Useful for refreshing a token before it expires, which would require the user to login again
  • [auth.ticket.verify] - Verifies a JWT token, and that the user is logged in, in addition to (optionally) that he belongs to one roles supplied as a comma separated list of roles
  • [] - The same as [auth.ticket.verify], but returns true or false, depending upon whether or not user is in any of the roles provided or not

Notice, you will have to modify your auth:secret configuration setting, to provide a unique salt for your installation. If you don't do this, some adversary can easily reproduce your tokens, and impersonate your users. Example of an "appsettings.json" secret you could apply can be found below (don't use the exact same salt, the idea is to provide a random salt, unique for your installation)

However, during installation, Magic will automatically create a random secret for you. Below is an example of how this might look like.

  "auth": {

The idea is that your SymmetricSecretKey is based upon the above configuration setting, implying you should keep it safe the same way you'd keep the pin code to your ATM card safe.

Below is an example of how to create a new JWT ticket.


The above will return something resembling the following, minus the carriage returns, which are added for clarity.


Typically Magic takes care of authentication for you, by exposing an authenticate endpoint for you, where you can submit a username and a password, for then to have your JWT token returned accordingly. You can also associate any claims you wish with your JWT token, by adding a key/value [claims] argument to your invocation as you create your JWT token. Below is an example.


All additional (non-roles) claims will have their values converted to string though, which is more of a restriction from .Net and the JWT standard then a restriction of Magic.

Notice - Also notice that if you create a JWT token with a user belonging to only one role, the roles claims will not be an array, but simply a string. Yet again, this is simply how .Net works, and there is little we can do about this. Hence, remember to check in your frontend as you retrieve your roles if the roles claims is a string or an array of strings before associating your user with a role in your client logic.

JWT tokens internals

A JWT token is actually just 3 JSON objects, that have been base64 encoded, and afterwards each entity is separated by a period (.). In fact, you can inspect a JWT token at for instance to inspect its internals. A JWT token has 3 parts.

  • The header providing meta information
  • The payload being the actual data of the token
  • The signature which is basically just a HASH based upon your token's payload, salted with your auth secret

Unless an adversary knows your auth secret, it becomes impossible to create a valid JWT token. However, your token is still vulnerable for being stolen, so make sure you only return it over a secure HTTP connection.

Using your JWT token

Magic expects the token to be specified as a Bearer token in your HTTP invocation's Authorization HTTP header. To use the token therefor implies adding an HTTP Authorization header, and setting its value to something resembling the following.

Authorization: Bearer

You can see how Magic does this internally by inspecting what the Magic dashboard transmits to the backend after having authenticated using e.g. Chrome Developer Tools.

Refreshing your JWT token

JWT tokens have an expiration date. This is typically configured in your "appsettings.json" file in your Magic backend as magic:auth:valid-minutes, and once this time has passed, the token will no longer be considered valid, and using the token after this period, will result in an "Access Denied" being returned from the server.

However, since JWT token are just JSON, and the expiration time is just a Unix timestamp, you can parse the JWT token in your frontend, calculate the expiration date, and make sure you refresh your token before the expiration date is reached, and replace your existing token with the new token returned by Magic.

Magic contains a refresh token endpoint to achieve this. Internally this endpoint will use the [auth.ticket.refresh] slot to create a new token, where the only difference between your old and your new token is that it has a new expiration date.

Securing your endpoints

To secure an endpoint, and for instance require the user to belong to a specific role in order to be allowed to invoke an endpoint, you might add something such as the following to the top of your Hyperlambda files.

auth.ticket.verify:root, admin, user

The above will throw an exception unless the endpoint is given a valid JWT token, and the user belongs to one of the roles that you pass in as a comma separated list of role in its invocation. In fact, you can try this out, by pasting the following into your Hyperlambda evaluator in your dashboard and execute it.


Inspecting your JWT token's payload

The following slots allows you to inspect the token's payload, implying get the username and roles the current HTTP request originates from.

  • [auth.ticket.get] - Returns the username and the roles the user belongs to
  • [] - Convenience slot returning true if user belongs to the specified role(s)

Below is an example of how these are used., root

The first slot return true because your root user belongs to one of the roles you supplied as a comma separated list of roles. The second slot above returns the username, and all the roles the user belongs to as a structured lambda object. As a general rule of thumb though, you'd not want to secure your endpoints using the above slots, but rather the verify slot above, to avoid having errors in your code resulting in that an unauthorized user gains access to invoke an endpoint he should not be allowed to invoke. The [auth.ticket.get] slot will also return any custom claims you have associated with your JWT token as it was created.

How to use [auth.ticket.get]

This slot returns the currently authenticated user, its roles, and what claims the user has. Below is example usage. The slot returns the username as the value of its invocation and the roles as a [roles] list.


Like all auth related slots, it only makes sense to invoke from within a context of having an authorised user, such as an HTTP endpoint intended to be invoked by an authenticated user.

How to use [auth.ticket.create]

This slot create a new JWT token for you, and accepts a username and optionally a list of roles. Below is some example usage.


Like all auth related slots, it only makes sense to invoke from within a context of having an authorised user, such as an HTTP endpoint intended to be invoked by an authenticated user.

How to use [auth.ticket.refresh]

This slot returns a new JWT token from an old existing (and valid) token. However, the new token will have a new expiration date, further into the future, allowing you to create a timer that occassionally invokes the server to create a new token, preventing the user from being logged out due to having his or her JWT token becoming expired.


Like all auth related slots, it only makes sense to invoke from within a context of having an authorised user, such as an HTTP endpoint intended to be invoked by an authenticated user.

How to use [auth.ticket.verify]

This slot verifies that the currently authenticated user belongs to one or more roles provided as a comma separated list of names. If the user does not belong to any of the specified roles, the slot will throw an exception. Below is example usage.


Like all auth related slots, it only makes sense to invoke from within a context of having an authorised user, such as an HTTP endpoint intended to be invoked by an authenticated user.

How to use []

This slot verifies that the currently authenticated user belongs to one or more roles provided as a comma separated list of names. Contrary to the [auth.ticket.verify] slot this slot will not throw an exception if the user does not belong to one or more roles, but simply return false. Below is example usage.,admin,guest

Like all auth related slots, it only makes sense to invoke from within a context of having an authorised user, such as an HTTP endpoint intended to be invoked by an authenticated user.

Project website for magic.lambda.caching

The source code for this repository can be found at, and you can provide feedback, provide bug reports, etc at the same place.

  • Build status
  • Quality Gate Status
  • Bugs
  • Code Smells
  • Coverage
  • Duplicated Lines (%)
  • Lines of Code
  • Maintainability Rating
  • Reliability Rating
  • Security Rating
  • Technical Debt
  • Vulnerabilities
Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on magic.lambda.auth:

Package Downloads

Helper project for Magic to wire up everything easily by simply adding one package, and invoking two simple methods. When using Magic, this is (probably) the only package you should actually add, since this package pulls in everything else you'll need automatically, and wires up everything sanely by default. To use package go to

Active Directory authentication helper project for Magic, giving you support for authenticating over AD from Hyperlambda. To use package go to

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
15.8.2 64 3/20/2023
15.7.0 98 3/6/2023
15.5.0 904 1/28/2023
15.2.0 394 1/18/2023
15.1.0 915 12/28/2022
14.5.7 464 12/13/2022
14.5.5 554 12/6/2022
14.5.1 435 11/23/2022
14.5.0 386 11/18/2022
14.4.5 475 10/22/2022
14.4.1 485 10/22/2022
14.4.0 437 10/17/2022
14.3.1 900 9/12/2022
14.3.0 450 9/10/2022
14.1.3 721 8/7/2022
14.1.2 463 8/7/2022
14.1.1 460 8/7/2022
14.0.14 512 7/26/2022
14.0.12 479 7/24/2022
14.0.11 480 7/23/2022
14.0.10 481 7/23/2022
14.0.9 488 7/23/2022
14.0.8 565 7/17/2022
14.0.5 628 7/11/2022
14.0.4 590 7/6/2022
14.0.3 535 7/2/2022
14.0.2 488 7/2/2022
14.0.0 613 6/25/2022
13.4.0 1,542 5/31/2022
13.3.4 1,115 5/9/2022
13.3.0 729 5/1/2022
13.2.0 876 4/21/2022
13.1.0 799 4/7/2022
13.0.0 554 4/5/2022
11.0.5 1,057 3/2/2022
11.0.4 493 2/22/2022
11.0.3 502 2/9/2022
11.0.2 515 2/6/2022
11.0.1 503 2/5/2022
10.0.21 489 1/28/2022
10.0.20 488 1/27/2022
10.0.19 495 1/23/2022
10.0.18 465 1/17/2022
10.0.15 614 12/31/2021
10.0.14 353 12/28/2021
10.0.7 968 12/22/2021
10.0.5 486 12/18/2021
9.9.9 1,281 11/29/2021
9.9.3 574 11/9/2021
9.9.2 402 11/4/2021
9.9.0 519 10/30/2021
9.8.9 485 10/29/2021
9.8.7 430 10/27/2021
9.8.6 407 10/27/2021
9.8.5 498 10/26/2021
9.8.1 789 10/21/2021
9.8.0 441 10/20/2021
9.7.9 430 10/19/2021
9.7.7 721 10/17/2021
9.7.5 628 10/14/2021
9.7.0 590 10/9/2021
9.6.6 866 8/14/2021
9.2.0 4,743 5/26/2021
9.1.4 882 4/21/2021
9.1.0 686 4/14/2021
9.0.0 604 4/5/2021
8.9.9 738 3/30/2021
8.9.3 1,203 3/19/2021
8.9.2 740 1/29/2021
8.9.1 739 1/24/2021
8.9.0 827 1/22/2021
8.6.9 2,414 11/8/2020
8.6.6 1,560 11/2/2020
8.6.0 3,144 10/28/2020
8.5.0 1,479 10/23/2020
8.4.0 4,487 10/13/2020
8.3.1 2,125 10/5/2020
8.3.0 962 10/3/2020
8.2.2 1,540 9/26/2020
8.2.1 975 9/25/2020
8.2.0 1,012 9/25/2020
8.1.17 5,477 9/13/2020
8.1.16 424 9/13/2020
8.1.15 1,442 9/12/2020
8.1.11 1,972 9/11/2020
8.1.10 982 9/6/2020
8.1.9 971 9/3/2020
8.1.8 965 9/2/2020
8.1.7 873 8/28/2020
8.1.4 892 8/25/2020
8.1.3 934 8/18/2020
8.1.2 868 8/16/2020
8.1.1 968 8/15/2020
8.1.0 414 8/15/2020
8.0.1 2,066 8/7/2020
8.0.0 932 8/7/2020
7.0.1 1,056 6/28/2020
7.0.0 935 6/28/2020
5.0.0 6,016 2/25/2020
4.0.4 6,357 1/27/2020
4.0.3 971 1/27/2020
4.0.2 1,078 1/16/2020
4.0.1 1,060 1/11/2020
4.0.0 1,049 1/5/2020
3.1.0 5,177 11/10/2019
3.0.0 3,121 10/23/2019
2.0.2 4,916 10/18/2019
2.0.1 2,295 10/15/2019
2.0.0 1,235 10/13/2019
1.1.9 994 10/11/2019
1.1.8 997 10/10/2019
1.1.7 417 10/9/2019
1.1.6 416 10/7/2019
1.1.5 421 10/6/2019
1.1.4 425 10/6/2019
1.1.2 429 10/5/2019