authentication-server-side.js revision 4701cb94cccc5ef52c5333593fe9dd5a2376a9d9
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Joostevar START_TIME = 9; // 9am
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Joostevar END_TIME = 17; // 5pm
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Joostelogger.message("Starting authentication javascript");
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Joostelogger.message("User: " + username);
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste// Log out current cookies in the request
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Joosteif (logger.messageEnabled()) {
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste var cookies = requestData.getHeaders('Cookie');
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste for (cookie in cookies) {
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste logger.message('Cookie: ' + cookies[cookie]);
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste }
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste}
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Joosteif (username) {
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste // Fetch user information via REST
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste var response = httpClient.get("http://localhost:8080/openam/json/users/" + username, {
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste cookies : [],
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste headers : []
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste });
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste // Log out response from REST call
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste logger.message("User REST Call. Status: " + response.getStatusCode() + ", Body: " + response.getEntity());
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste}
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Joostevar now = new Date();
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Joostelogger.message("Current time: " + now.getHours());
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Joosteif (now.getHours() < START_TIME || now.getHours() > END_TIME) {
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste logger.error("Login forbidden outside work hours!");
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste authState = FAILED;
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste} else {
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste logger.message("Authentication allowed!");
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste authState = SUCCESS;
4701cb94cccc5ef52c5333593fe9dd5a2376a9d9Jaco Jooste}