<%

/*
 * Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *                                                                         
 * Licensed under the Apache License, Version 2.0 (the "License");         
 * you may not use this file except in compliance with the License.        
 * You may obtain a copy of the License at                                 
 *                                                                         
 * http://www.apache.org/licenses/LICENSE-2.0                              
 *                                                                         
 * Unless required by applicable law or agreed to in writing, software     
 * distributed under the License is distributed on an "AS IS" BASIS,       
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and     
 * limitations under the License.
 */

include("util/constants.jag");
var util = require("util/utility.jag");

var log = new Log();
var multitenantUtils = Packages.org.wso2.carbon.utils.multitenancy.MultitenantUtils;

var userStoreDomain = PRIMARY_DEFAULT_DOMAIN_NAME;

var sessionId = request.getParameter("sessionId");
var incomingUserName = request.getParameter("userOfSession");
var loggedInUser = session.get(LOGGED_IN_USER);

var tenantDomain = multitenantUtils.getTenantDomain(loggedInUser);

// Remove tenant domain from the username.
var username = multitenantUtils.getTenantAwareUsername(incomingUserName);

// Extract the user store domain from the username, if there is one.
var usernameTokens = username.split('/');
if (usernameTokens.length > 1) {
    userStoreDomain = usernameTokens[0];
    username = usernameTokens[1];
}

var log = new Log();
var carbon = require('carbon');
var userSessionManagementService = carbon.server.osgiService('org.wso2.carbon.identity.application.authentication.framework.UserSessionManagementService');

var userObj = new Packages.org.wso2.carbon.identity.application.common.model.User();
userObj.setUserName(username);
userObj.setTenantDomain(tenantDomain);
userObj.setUserStoreDomain(userStoreDomain);

try {

    log.info("Terminating session:" + sessionId + "of user:" + username);
    userSessionManagementService.terminateSessionBySessionId(userObj, -1, sessionId);

    //Get remaining sessions of the user in local idp.
    log.info("Fetching session data for the user: " + username + " in userstore: " + userStoreDomain);
    var userSessions = userSessionManagementService.getSessionsByUser(userObj, -1);
    var tableRows = new Array();
    for (var i = 0; i < userSessions.size(); i++) {
        var mySession = userSessions.get(i);
        var obj = {};
        obj.userName = incomingUserName;
        obj.IP = mySession.getIp();
        obj.userAgent = mySession.getUserAgent();
        obj.startTimestamp = new Date(Number(mySession.getLoginTime())).toString();
        obj.sessionId = mySession.getSessionId();

        tableRows [i] = obj;
    }
    print(tableRows);
} catch (e) {
    log.error(e);
    response.sendError(500);
}

%>
