All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.camunda.bpm.extension.keycloak.KeycloakContext Maven / Gradle / Ivy

There is a newer version: 2.2.3
Show newest version
package org.camunda.bpm.extension.keycloak;

import org.camunda.bpm.extension.keycloak.util.ContentType;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;

/**
 * Keycloak context holding authorization header / access token.
 */
public class KeycloakContext {

	private HttpHeaders headers;
	
	private long expiresAt;
	
	String refreshToken;
	
	public KeycloakContext(String accessToken, String tokenType, long expiresInMillis, String refreshToken, String charset) {
		headers = new HttpHeaders();
		headers.add(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON + ";charset="+charset);
		headers.add(HttpHeaders.AUTHORIZATION, tokenType + " " + accessToken);
		expiresAt = System.currentTimeMillis() + expiresInMillis - 2000;
		this.refreshToken = refreshToken;
	}
	
	/**
	 * Creates a new HTTP Request entity including the authorization header.
	 * @return the request entity
	 */
	public HttpEntity createHttpRequestEntity() {
		return new HttpEntity<>(headers);
	}
 	
	/**
	 * Whether the access token needs to be refreshed
	 * @return {@code true} in case a refresh is required
	 */
	public boolean needsRefresh() {
		return System.currentTimeMillis() >= expiresAt;
	}

	/**
	 * The refresh token
	 * @return the refreshToken
	 */
	public String getRefreshToken() {
		return refreshToken;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy