Authentication

abstract class Authentication<P> : Middleware

Special Middleware to use in at http API to provide an authentication for every request. The type-parameter P represents the principal information, which contains all login-information needed to authenticate against the external HTTP API. The principal information is held inside and available as flow by calling data. To use this Middleware you need to implement the addAuthentication method, where you get the principal information, when available, to add it to your requests. To implement the client side of authentication process you also need to implement the authenticate method in which you specify what is needed to authenticate the user (e.g. open up a login modal). When your authentication process is done you have to call the complete function and set your principal. Then all requests, that have been initialized while the authentication was running, will be re-executed with the additional authentication information provided by the addAuthentication method. When the user logs out you have to call the clear function to clear all authentication information.

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard

flow of the information whether the user is authenticated or not

Link copied to clipboard
val current: P?

Returns the current principal information. When the principal is available it is returned. When an authentication-process is running the function is waiting for the process to be finished. Otherwise, it returns null if no authentication-process is running or no principal information is available.

Link copied to clipboard
val data: Flow<P?>

flow of the current principal P

Link copied to clipboard

List of HTTP-Status-Codes forcing an authentication. Default is 401 (unauthorized).

Functions

Link copied to clipboard
abstract fun addAuthentication(request: Request, principal: P?): Request

Adds the authentication information to all requests by using the given principal.

Link copied to clipboard
abstract fun authenticate()

implements the authentication process. E.g. opens up a login-modal or browsing to the login-page.

Link copied to clipboard
fun clear()

clears the current principal information. Needed when performing a logout.

Link copied to clipboard
fun complete(principal: P)

completes the authentication process by setting the principal. When no authentication process is running it only sets the principal.

Link copied to clipboard
suspend override fun enrichRequest(request: Request): Request

enriches requests with additional information. E.g. it could append special header-information, which are needed for authentication.

Link copied to clipboard
suspend override fun handleResponse(response: Response): Response

handles responses. E.g. it could handle specific status-codes and log error messages.

Link copied to clipboard
fun start()

starts the authentication process.

Link copied to clipboard

stops propagation of a response to the following Middlewares in the chain