/**
 * License:  AGPLv3
 * This file is part of eduMFA. eduMFA is a fork of privacyIDEA.
 *
 * 2015 Cornelius Kölbel, <cornelius@privacyidea.org>
 *
 * (c) cornelius kölbel, cornelius@privacyidea.org
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
myApp.factory("PolicyTemplateFactory", ["$http", "inform", "gettextCatalog",
                                        function($http, inform, gettextCatalog){
    var URL = "https://raw.githubusercontent.com/eduMFA/policy-templates/main/templates/";
    return {
        setUrl: function(url) {
            URL = url;
        },
        getTemplates: function(callback) {
            //debug: console.log("Going to fetch Policy Templates");
            $http.get(URL + "index.json")
                .then(function(response) { callback(response.data) }, function (error) {
                    //debug: console.log("Error fetching Policy Templates.");
                    //debug: console.log(error);
                    inform.add(gettextCatalog.getString("Error fetching" +
                        " policy templates."),
                                {type: "danger", ttl:10000});
            });
        },
        getTemplate: function(templateName, callback) {
            //debug: console.log("Going to fetch Policy Template " + templateName);
            $http.get(URL + templateName + ".json")
                .then(function(response) { callback(response.data) }, function (error) {
                    //debug: console.log(error);
                    inform.add(gettextCatalog.getString("Error fetching" +
                            " policy template ")
                        + templateName,
                                {type: "danger", ttl:10000});
            });
        }
    };
}]);

myApp.factory("ConfigFactory", ["AuthFactory", "$http", "$state", "$rootScope",
                                "resolverUrl", "realmUrl", "machineResolverUrl",
                                "policyUrl", "eventUrl", "smtpServerUrl",
                                "radiusServerUrl", "smsgatewayUrl",
                                "defaultRealmUrl", "systemUrl", "periodicTaskUrl",
                                "eduMfaServerUrl", "CAConnectorUrl", "tokengroupUrl",
                                "serviceidUrl",
                                function (AuthFactory, $http, $state, $rootScope,
                                          resolverUrl, realmUrl, machineResolverUrl,
                                          policyUrl, eventUrl, smtpServerUrl,
                                          radiusServerUrl, smsgatewayUrl,
                                          defaultRealmUrl, systemUrl,
                                          periodicTaskUrl, eduMfaServerUrl,
                                          CAConnectorUrl, tokengroupUrl, serviceidUrl) {
    /**
     Each service - just like this service factory - is a singleton.
     */
    return {
        getPGPKeys: function(callback) {
            $http.get(systemUrl + "/gpgkeys", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getSMSProviders: function(callback) {
            $http.get(smsgatewayUrl + "/providers", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getSMSGateways: function(gwid, callback) {
            if(!gwid) {gwid = "";}
            $http.get(smsgatewayUrl + "/" + gwid, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        setSMSGateway: function(params, callback) {
            $http.post(smsgatewayUrl, params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delSMSGateway: function(name, callback) {
            $http.delete(smsgatewayUrl+ "/" + name, {
                headers: {'Authorization': AuthFactory.getAuthToken()}}
            ).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delEvent: function(eventId, callback) {
            $http.delete(eventUrl + "/" + eventId, {
                headers: {'Authorization': AuthFactory.getAuthToken()}}
            ).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        enableEvent: function(eventId, callback) {
            $http.post(eventUrl + "/enable/" + eventId, {},
                {
                    headers: {'Authorization': AuthFactory.getAuthToken()}}
            ).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        disableEvent: function(eventId, callback) {
            $http.post(eventUrl + "/disable/" + eventId, {},
                {
                    headers: {'Authorization': AuthFactory.getAuthToken()}}
            ).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        setEvent: function(params, callback) {
            $http.post(eventUrl, params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getEvents: function(callback) {
            $http.get(eventUrl + "/", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getEvent: function(eventid, callback) {
            $http.get(eventUrl + "/" + eventid, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getHandlerActions: function(handlername, callback) {
            $http.get(eventUrl + "/actions/" + handlername, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getHandlerConditions: function(handlername, callback) {
            $http.get(eventUrl + "/conditions/" + handlername, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getHandlerPositions: function(handlername, callback) {
            $http.get(eventUrl + "/positions/" + handlername, {
              headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delPeriodicTask: function (ptaskid, callback) {
            $http.delete(periodicTaskUrl + "/" + ptaskid, {
                headers: {'Authorization': AuthFactory.getAuthToken()}}
            ).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getPeriodicTasks: function(callback) {
            $http.get(periodicTaskUrl + "/", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getPeriodicTaskmodules: function(callback) {
            $http.get(periodicTaskUrl + "/taskmodules/", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getPeriodicTaskmoduleOptions: function(taskmodule, callback) {
            $http.get(periodicTaskUrl + "/options/" + taskmodule, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getPeriodicTask: function(ptaskid, callback) {
            $http.get(periodicTaskUrl + "/" + ptaskid, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        setPeriodicTask: function(params, callback) {
            $http.post(periodicTaskUrl + "/", params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        enablePeriodicTask: function(ptaskid, callback) {
            $http.post(periodicTaskUrl + "/enable/" + ptaskid, {},
                { headers: {'Authorization': AuthFactory.getAuthToken()} }
            ).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        disablePeriodicTask: function(ptaskid, callback) {
            $http.post(periodicTaskUrl + "/disable/" + ptaskid, {},
                { headers: {'Authorization': AuthFactory.getAuthToken()} }
            ).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getNodes: function(callback) {
            $http.get(periodicTaskUrl + "/nodes/", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delPolicy: function (policyName, callback) {
            $http.delete(policyUrl + "/" + policyName, {
                headers: {'Authorization': AuthFactory.getAuthToken()}}
            ).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        setPolicy: function (policyName, params, callback) {
            $http.post(policyUrl + "/" + policyName, params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getPolicies: function (callback) {
            $http.get(policyUrl + "/", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getPolicy: function (policyname, callback) {
            $http.get(policyUrl + "/" + policyname, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        enablePolicy: function (policyname, callback) {
            $http.post(policyUrl + "/enable/" + policyname, {}, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        disablePolicy: function (policyname, callback) {
            $http.post(policyUrl + "/disable/" + policyname, {}, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getPolicyDefs: function (callback) {
            // Return the policy definitions
            $http.get(policyUrl + "/defs", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getPolicyConditionDefs: function (callback) {
            // Return the definitions for policy conditions
            $http.get(policyUrl + "/defs/conditions", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getEduMfaNodes: function (callback) {
            // Return the definitions for policy conditions
            $http.get(policyUrl + "/defs/edumfanodes", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getResolvers: function (callback) {
            $http.get(resolverUrl + "/", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getEditableResolvers: function (callback) {
            $http.get(resolverUrl + "/?editable=1", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getResolver: function(resolvername, callback) {
            $http.get(resolverUrl + "/" + resolvername, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getMachineResolver: function (resolvername, callback) {
            $http.get(machineResolverUrl + "/" + resolvername, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getMachineResolvers: function (callback) {
            $http.get(machineResolverUrl + "/", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getCAConnectors: function (callback) {
            $http.get(CAConnectorUrl + "/", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getCAConnectorNames: function (callback) {
            $http.get(systemUrl + "/names/caconnector", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getCAConnector: function (connectorname, callback) {
            $http.get(CAConnectorUrl + "/" + connectorname, {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getRealms: function (callback) {
            $http.get(realmUrl + "/", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getAdminRealms: function(callback) {
            $http.get(realmUrl + "/superuser", {
                headers: {'Authorization': AuthFactory.getAuthToken()}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        setResolver: function (name, params, callback) {
            $http.post(resolverUrl + "/" + name, params,
                {headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
                }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        setMachineResolver: function (name, params, callback) {
            $http.post(machineResolverUrl + "/" + name, params,
                {headers: {'Authorization': AuthFactory.getAuthToken(),
                           'Content-Type': 'application/json'}
                }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        setCAConnector: function(name, params, callback) {
            $http.post(CAConnectorUrl + "/" + name, params,
                {headers: {'Authorization': AuthFactory.getAuthToken(),
                           'Content-Type': 'application/json'}
                }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getCASpecificOptions: function(catype, params, callback) {
            //encodeURI("/random?len=" + len + "&encode=" + encode)
            var pstring = new URLSearchParams(params).toString()
            $http.get(CAConnectorUrl + "/specific/" + catype + "?" + pstring,
                {headers: {'Authorization': AuthFactory.getAuthToken()}
                }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        testResolver: function (params, callback) {
            $http.post(resolverUrl + "/test", params,
                {headers: {'Authorization': AuthFactory.getAuthToken(),
                           'Content-Type': 'application/json'}
                }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        testMachineResolver: function (params, callback) {
            $http.post(machineResolverUrl + "/test", params,
                {headers: {'Authorization': AuthFactory.getAuthToken(),
                           'Content-Type': 'application/json'}
                }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delResolver: function(name, callback) {
            $http.delete(resolverUrl + "/" + name, {
                headers: {'Authorization': AuthFactory.getAuthToken() }
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });

        },
        delMachineResolver: function(name, callback) {
            $http.delete(machineResolverUrl + "/" + name, {
                headers: {'Authorization': AuthFactory.getAuthToken() }
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });

        },
        delCAConnector: function(name, callback) {
            $http.delete(CAConnectorUrl + "/" + name, {
                headers: {'Authorization': AuthFactory.getAuthToken() }
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        setRealm: function(name, params, callback) {
            $http.post(realmUrl + "/" + name, params, {
                    headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delRealm: function(name, callback) {
            $http.delete(realmUrl +  "/" + name, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        setDefaultRealm: function(name, callback) {
            $http.post(defaultRealmUrl + "/" + name, {},
                {headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) })
        },
        clearDefaultRealm: function(callback) {
            $http.delete(defaultRealmUrl, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getDocumentation: function(callback) {
            $http.get(systemUrl + "/documentation", {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        saveSystemConfig: function(params, callback) {
            $http.post(systemUrl + "/setConfig", params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delSystemConfig: function(key, callback) {
            $http.delete(systemUrl + "/" + key, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getRandom: function(len, encode, callback) {
            $http.get(systemUrl +
                encodeURI("/random?len=" + len + "&encode=" + encode), {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        testTokenConfig: function(tokentype, params, callback) {
            $http.post(systemUrl + "/test/" + tokentype, params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        loadSystemConfig: function(callback, key) {
            if (!key) {key = "";}
            $http.get(systemUrl + "/" + key, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getSystemConfig: function(callback) {
            $http.get(systemUrl + "/", {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getSmtp: function(callback, identifier) {
            if (!identifier) {identifier = "";}
            $http.get(smtpServerUrl + "/" + identifier, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delSmtp: function(identifier, callback) {
            $http.delete(smtpServerUrl + "/" + identifier, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        addSmtp: function(params, callback) {
            var identifier = params["identifier"];
            $http.post(smtpServerUrl + "/" + identifier, params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        testSmtp: function(params, callback) {
            $http.post(smtpServerUrl + "/send_test_email", params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getRadius: function(callback, identifier) {
            if (!identifier) {identifier = "";}
            $http.get(radiusServerUrl + "/" + identifier, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getRadiusNames: function(callback) {
            $http.get(systemUrl + "/names/radius", {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delRadius: function(identifier, callback) {
            $http.delete(radiusServerUrl + "/" + identifier, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        addRadius: function(params, callback) {
            var identifier = params["identifier"];
            $http.post(radiusServerUrl + "/" + identifier, params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        testRadius: function(params, callback) {
            $http.post(radiusServerUrl + "/test_request", params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getTokengroup: function(groupname, callback) {
            $http.get(tokengroupUrl + "/" + groupname, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        addTokengroup: function(params, callback) {
            $http.post(tokengroupUrl + "/" + params["groupname"], params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delTokengroup: function(groupname, callback) {
            $http.delete(tokengroupUrl + "/" + groupname, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getServiceid: function(sname, callback) {
            $http.get(serviceidUrl + "/" + sname, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        addServiceid: function(params, callback) {
            $http.post(serviceidUrl + "/" + params["servicename"], params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delServiceid: function(sname, callback) {
            $http.delete(serviceidUrl + "/" + sname, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        getEduMfaServer: function(callback, identifier) {
            if (!identifier) {identifier = "";}
            $http.get(eduMfaServerUrl + "/" + identifier, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        delEduMfaServer: function(identifier, callback) {
            $http.delete(eduMfaServerUrl + "/" + identifier, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        addEduMfaServer: function(params, callback) {
            var identifier = params["identifier"];
            $http.post(eduMfaServerUrl + "/" + identifier, params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        },
        testEduMfaServer: function(params, callback) {
            $http.post(eduMfaServerUrl + "/test_request", params, {
                headers: {'Authorization': AuthFactory.getAuthToken(),
                          'Content-Type': 'application/json'}
            }).then(function(response) { callback(response.data) }, function(error) { AuthFactory.authError(error.data) });
        }
    };
}]);
