Skip to content

Authentication

Tambapps edited this page Jan 11, 2022 · 8 revisions

This library provides a header auth utilities.

You can provide auth to a poet, so that every request will be authenticated

Basic Authentication

Basic Auth allows you to authenticate by providing your user and password in a header put in all your requests. You can do so with the following code example

poet.putHeader(Headers.basicAuth("user", "password"))

Jwt Authentication (with refresh)

JWT authentication consits in passing a token that will allow the server to authenticate your request. The token may expire so it will need to be refreshed after some time. You can provide JWT auth to a poet, with automatic token refreshening with the JwtAuthInterceptor

 poet.addInterceptor(new JwtAuthInterceptor({ HttpPoet it ->
      def auth = it.post('https://my-api.com/new/token', contentType: ContentType.JSON, body: [user: 'user', password: 'password'])
      return auth.access_token
    }))

The closure passed in parameter will be called every time the token is expired. The HttpPoet it is not the poet you created. It is a new poet created in the JwtAuthInterceptor with no base URL, so that you can call your auth endpoint without having to worry about precedent auth of your current poet.

You can refresh credentials with the given example

poet.interceptor.refreshToken()

HttpPoet.interceptor returns the first interceptor you added, in this case the JwtAuthInterceptor. If you added more than one, you can stil access them by index with poet.interceptors[index].

Or, if you just want a JWT auth without having to worry about refreshing your token you can just provide the header

poet.putHeader(Headers.jwt(myToken))
Clone this wiki locally