blob: dd2448c1d0f6f06ee75a1ef895652f5ef677aaa4 [file] [log] [blame]
<%
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
*/
(function () {
var log = new Log();
var HTTPS_TRANSPORT = "https";
var AUTHORIZATION_HEADER = "Authorization";
var ANALYTICS_SERVICE = "/services/AnalyticsWebService";
var AUTHENTICATION_SERVICE = "/services/AuthenticationAdmin";
var AUTH_TOKEN = "authToken";
var LOCALHOST = "localhost";
var HTTP_USER_NOT_AUTHENTICATED = 403;
var carbon = require('carbon');
var configs = require('/configs/designer.json');
var HTTPConstants = Packages.org.apache.axis2.transport.http.HTTPConstants;
var AnalyticsWebServiceStub = Packages.org.wso2.carbon.analytics.webservice.stub.AnalyticsWebServiceStub;
var AuthenticationAdminStub = Packages.org.wso2.carbon.authenticator.stub.AuthenticationAdminStub;
var analyticsWSUrl = carbon.server.address(HTTPS_TRANSPORT) + ANALYTICS_SERVICE;
var authenticationWSUrl = carbon.server.address(HTTPS_TRANSPORT) + AUTHENTICATION_SERVICE;
var analyticsStub;
try {
analyticsStub = new AnalyticsWebServiceStub(analyticsWSUrl);
} catch (e) {
if (log.isDebugEnabled()) {
log.debug("Unable to instantiate AnalyticsWebServiceStub instance. This could be a CEP server. Returning");
}
return;
}
var serviceClient = analyticsStub._getServiceClient();
var options = serviceClient.getOptions();
options.setManageSession(true);
var authParam = request.getHeader(AUTHORIZATION_HEADER);
if (authParam != null) {
credentials = JSUtils.authenticate(authParam);
authenticationAdminStub = new AuthenticationAdminStub(authenticationWSUrl);
authenticationAdminStub.login(credentials[0], credentials[1], LOCALHOST);
var serviceContext = authenticationAdminStub._getServiceClient().getLastOperationContext()
.getServiceContext();
var sessionCookie = serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
options.setProperty(HTTPConstants.COOKIE_STRING, sessionCookie);
} else {
var token = session.get(AUTH_TOKEN);
if (token != null) {
options.setProperty(HTTPConstants.COOKIE_STRING, token);
} else {
log.error("user is not authenticated!");
response.status = HTTP_USER_NOT_AUTHENTICATED;
print('{ "status": "Failed", "message": "User is not authenticated." }');
return;
}
}
});
%>