Passed
Push — master ( 9b05f5...390067 )
by Stefan
03:53
created

return_success()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * ******************************************************************************
5
 * Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 
6
 * and GN4-2 consortia
7
 *
8
 * License: see the web/copyright.php file in the file structure
9
 * ******************************************************************************
10
 */
11
?>
12
<?php
13
14
require_once(dirname(dirname(dirname(__FILE__))) . "/config/_config.php");
15
16
17
// no SAML auth on this page. The API key authenticates the entity
18
19
$mode = "API";
20
21
$adminApi = new \web\lib\admin\API();
22
$validator = new \web\lib\common\InputValidation();
23
$optionParser = new \web\lib\admin\OptionParser();
24
25
if (!isset(CONFIG['registration_API_keys']) || count(CONFIG['registration_API_keys']) == 0) {
26
    $adminApi->return_error(web\lib\admin\API::ERROR_API_DISABLED, "API is disabled in this instance of CAT");
27
}
28
29
$inputRaw = file_get_contents('php://input');
30
$inputDecoded = json_decode($inputRaw, TRUE);
31
if (!is_array($inputDecoded)) {
32
    $adminApi->return_error(web\lib\admin\API::ERROR_MALFORMED_REQUEST, "Unable to decode JSON POST data.");
33
}
34
35
if (!isset($inputDecoded['APIKEY'])) {
36
    $adminApi->return_error(web\lib\admin\API::ERROR_NO_APIKEY, "JSON request structure did not contain an APIKEY");
37
}
38
39
$checkval = "FAIL";
40
foreach (CONFIG['registration_API_keys'] as $key => $fed_name) {
41
    if ($inputDecoded['APIKEY'] == $key) {
42
        $mode = "API";
43
        $federation = $fed_name;
44
        $checkval = "OK-NEW";
45
    }
46
}
47
48
if ($checkval == "FAIL") {
49
    $adminApi->return_error(web\lib\admin\API::ERROR_INVALID_APIKEY, "APIKEY is invalid");
50
}
51
52
// let's instantiate the fed, we will need it later
53
$fed = new \core\Federation($federation);
54
// it's a valid admin; what does he want to do?
55
if (!array_key_exists($inputDecoded['ACTION'], web\lib\admin\API::ACTIONS)) {
56
    $adminApi->return_error(web\lib\admin\API::ERROR_NO_ACTION, "JSON request structure did not contain a valid ACTION");
57
}
58
// it's a valid ACTION, so let's sanitise the input parameters
59
$scrubbedParameters = $adminApi->scrub($inputDecoded, $fed);
60
$paramNames = [];
61
foreach ($scrubbedParameters as $oneParam) {
62
    $paramNames[] = $oneParam['NAME'];
63
}
64
// are all the required parameters (still) in the request?
65
foreach (web\lib\admin\API::ACTIONS[$inputDecoded['ACTION']]['REQ'] as $oneRequiredAttribute) {
66
    if (!in_array($oneRequiredAttribute, $paramNames)) {
67
        $adminApi->return_error(web\lib\admin\API::ERROR_MISSING_PARAMETER, "At least one required parameter for this ACTION is missing: $oneRequiredAttribute");
68
    }
69
}
70
71
switch ($inputDecoded['ACTION']) {
72
    case web\lib\admin\API::ACTION_NEWINST:
73
        // create the inst, no admin, no attributes
74
        $idp = new \core\IdP($fed->newIdP("PENDING", "API"));
75
        // now add all submitted attributes
76
        $inputs = $adminApi->uglify($scrubbedParameters);
77
        $optionParser->processSubmittedFields($idp, $inputs["POST"], $inputs["FILES"]);
78
        $adminApi->return_success([web\lib\admin\API::AUXATTRIB_CAT_INST_ID => $idp->identifier]);
79
        break;
80
    case web\lib\admin\API::ACTION_DELINST:
81
        try {
82
        $idp = $validator->IdP($adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_CAT_INST_ID));
83
        } catch(Exception $e) {
84
            $adminApi->return_error(web\lib\admin\API::ERROR_INVALID_PARAMETER, "IdP identifier does not exist!");
85
        }
86
        $idp->destroy();
87
        $adminApi->return_success([]);
88
        break;
89
    case web\lib\admin\API::ACTION_ADMIN_LIST:
90
        try {
91
        $idp = $validator->IdP($adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_CAT_INST_ID));
92
        } catch(Exception $e) {
93
            $adminApi->return_error(web\lib\admin\API::ERROR_INVALID_PARAMETER, "IdP identifier does not exist!");
94
        }
95
        $adminApi->return_success($idp->listOwners());
96
        break;
97
    case web\lib\admin\API::ACTION_ADMIN_ADD:
98
        // IdP in question
99
        try {
100
        $idp = $validator->IdP($adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_CAT_INST_ID));
101
        } catch(Exception $e) {
102
            $adminApi->return_error(web\lib\admin\API::ERROR_INVALID_PARAMETER, "IdP identifier does not exist!");
103
        }
104
        // here is the token
105
        $mgmt = new core\UserManagement();
106
        // we know we have an admin ID but scrutinizer wants this checked more explicitly
107
        $admin = $adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_ADMINID);
108
        if ($admin === FALSE) {
109
            throw new Exception("A required parameter is missing, and this wasn't caught earlier?!");
110
        }
111
        $newtoken = $mgmt->createToken(true, $admin, $idp);
112
        $URL = "https://" . $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) . "/action_enrollment.php?token=$newtoken";
113
        $success = ["TOKEN URL" => $URL];
114
        // done with the essentials - display in response. But if we also have an email address, send it there
115
        $email = $adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_ADMINEMAIL);
116
        if ($email !== FALSE) {
117
            $sent = \core\common\OutsideComm::adminInvitationMail($email, "EXISTING-FED", $newtoken, $idp->name, $fed);
118
            $success["EMAIL SENT"] = $sent["SENT"];
119
            if ($sent["SENT"] === TRUE) {
120
                $success["EMAIL TRANSPORT SECURE"] = $sent["TRANSPORT"];
121
            }
122
        }
123
        $adminApi->return_success($success);
124
        break;
125
    case web\lib\admin\API::ACTION_ADMIN_DEL:
126
        // IdP in question
127
        try {
128
        $idp = $validator->IdP($adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_CAT_INST_ID));
129
        } catch(Exception $e) {
130
            $adminApi->return_error(web\lib\admin\API::ERROR_INVALID_PARAMETER, "IdP identifier does not exist!");
131
        }
132
        $currentAdmins = $idp->listOwners();
133
        $toBeDeleted = $adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_ADMINID);
134
        if ($toBeDeleted === FALSE) {
135
            throw new Exception("A required parameter is missing, and this wasn't caught earlier?!");
136
        }
137
        $found = FALSE;
138
        foreach($currentAdmins as $oneAdmin) {
139
            if ($oneAdmin['MAIL'] == $toBeDeleted) {
140
                $found = TRUE;
141
                $mgmt = new core\UserManagement();
142
                $mgmt->removeAdminFromIdP($idp, $oneAdmin['ID']);
143
            }
144
        }
145
        if ($found) {
146
            $adminApi->return_success([]);
147
        }
148
        $adminApi->return_error(web\lib\admin\API::ERROR_INVALID_PARAMETER, "The admin with ID $toBeDeleted is not associated to IdP ".$idp->identifier);
149
        break;
150
    case web\lib\admin\API::ACTION_STATISTICS_FED:
151
        $adminApi->return_success($fed->downloadStats("array"));
152
        break;
153
    default:
154
        $adminApi->return_error(web\lib\admin\API::ERROR_INVALID_ACTION, "Not implemented yet.");
155
}
156