2021-03-11 13:38:35 -08:00
|
|
|
package react.auth;
|
|
|
|
|
|
|
|
import com.linkedin.common.urn.CorpuserUrn;
|
|
|
|
import play.mvc.Http;
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
|
|
|
|
|
public class AuthUtils {
|
|
|
|
|
2021-06-25 07:24:27 +02:00
|
|
|
public static final String SESSION_TTL_CONFIG_PATH = "auth.session.ttlInHours";
|
|
|
|
public static final Integer DEFAULT_SESSION_TTL_HOURS = 720;
|
2021-03-11 13:38:35 -08:00
|
|
|
public static final CorpuserUrn DEFAULT_ACTOR_URN = new CorpuserUrn("datahub");
|
|
|
|
|
|
|
|
public static final String LOGIN_ROUTE = "/login";
|
|
|
|
public static final String USER_NAME = "username";
|
|
|
|
public static final String PASSWORD = "password";
|
|
|
|
public static final String ACTOR = "actor";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a request is authenticated, false otherwise.
|
|
|
|
*
|
|
|
|
* Note that we depend on the presence of 2 cookies, one accessible to the browser and one not,
|
|
|
|
* as well as their agreement to determine authentication status.
|
|
|
|
*/
|
|
|
|
public static boolean isAuthenticated(final Http.Context ctx) {
|
|
|
|
return ctx.session().containsKey(ACTOR)
|
|
|
|
&& ctx.request().cookie(ACTOR) != null
|
|
|
|
&& ctx.session().get(ACTOR).equals(ctx.request().cookie(ACTOR).value());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-25 07:24:27 +02:00
|
|
|
* Creates a client authentication cookie (actor cookie) with a specified TTL in hours.
|
2021-03-11 13:38:35 -08:00
|
|
|
*
|
|
|
|
* @param actorUrn the urn of the authenticated actor, e.g. "urn:li:corpuser:datahub"
|
2021-06-25 07:24:27 +02:00
|
|
|
* @param ttlInHours the number of hours until the actor cookie expires after being set
|
2021-03-11 13:38:35 -08:00
|
|
|
*/
|
2021-06-25 07:24:27 +02:00
|
|
|
public static Http.Cookie createActorCookie(final String actorUrn, final Integer ttlInHours) {
|
2021-03-11 13:38:35 -08:00
|
|
|
return Http.Cookie.builder(ACTOR, actorUrn)
|
|
|
|
.withHttpOnly(false)
|
2021-06-25 07:24:27 +02:00
|
|
|
.withMaxAge(Duration.of(ttlInHours, ChronoUnit.HOURS))
|
2021-03-11 13:38:35 -08:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
private AuthUtils() { }
|
|
|
|
|
|
|
|
}
|