Passed
Push — master ( 209205...a3611a )
by Stefan
03:23
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
function return_error($code, $description) {
26
    echo json_encode(["result" => "ERROR", "details" => ["errorcode" => $code, "description" => $description]], JSON_PRETTY_PRINT);
27
    exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
28
}
29
30
function return_success($details) {
31
    echo json_encode(["result" => "SUCCESS", "details" => $details], JSON_PRETTY_PRINT);
32
    exit(0);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
33
}
34
35
if (!isset(CONFIG['registration_API_keys']) || count(CONFIG['registration_API_keys']) == 0) {
36
    return_error(web\lib\admin\API::ERROR_API_DISABLED, "API is disabled in this instance of CAT");
37
}
38
39
$inputRaw = file_get_contents('php://input');
40
$inputDecoded = json_decode($inputRaw, TRUE);
41
if (!is_array($inputDecoded)) {
42
    return_error(web\lib\admin\API::ERROR_MALFORMED_REQUEST, "Unable to decode JSON POST data.");
43
}
44
45
if (!isset($inputDecoded['APIKEY'])) {
46
    return_error(web\lib\admin\API::ERROR_NO_APIKEY, "JSON request structure did not contain an APIKEY");
47
}
48
49
$checkval = "FAIL";
50
foreach (CONFIG['registration_API_keys'] as $key => $fed_name) {
51
    if ($inputDecoded['APIKEY'] == $key) {
52
        $mode = "API";
53
        $federation = $fed_name;
54
        $checkval = "OK-NEW";
55
    }
56
}
57
58
if ($checkval == "FAIL") {
59
    return_error(web\lib\admin\API::ERROR_INVALID_APIKEY, "APIKEY is invalid");
60
}
61
62
// let's instantiate the fed, we will need it later
63
$fed = new \core\Federation($federation);
64
// it's a valid admin; what does he want to do?
65
if (!array_key_exists($inputDecoded['ACTION'], web\lib\admin\API::ACTIONS)) {
66
    return_error(web\lib\admin\API::ERROR_NO_ACTION, "JSON request structure did not contain a valid ACTION");
67
}
68
// it's a valid ACTION, so let's sanitise the input parameters
69
$scrubbedParameters = $adminApi->scrub($inputDecoded);
70
$paramNames = [];
71
foreach ($scrubbedParameters as $oneParam) {
72
    $paramNames[] = $oneParam['NAME'];
73
}
74
// are all the required parameters (still) in the request?
75
foreach (web\lib\admin\API::ACTIONS[$inputDecoded['ACTION']]['REQ'] as $oneRequiredAttribute) {
76
    if (!in_array($oneRequiredAttribute, $paramNames)) {
77
        return_error(web\lib\admin\API::ERROR_MISSING_PARAMETER, "At least one required parameter for this ACTION is missing: $oneRequiredAttribute");
78
    }
79
}
80
81
switch ($inputDecoded['ACTION']) {
82
    case web\lib\admin\API::ACTION_NEWINST:
83
        // create the inst, no admin, no attributes
84
        $idp = new \core\IdP($fed->newIdP("PENDING", "API"));
85
        // now add all submitted attributes
86
        $inputs = $adminApi->uglify($scrubbedParameters);
87
        $optionParser->processSubmittedFields($idp, $inputs["POST"], $inputs["FILES"]);
88
        return_success([web\lib\admin\API::AUXATTRIB_CAT_INST_ID => $idp->identifier]);
89
        break;
90
    case web\lib\admin\API::ACTION_ADMIN_ADD:
91
        // IdP in question
92
        try {
93
        $idp = $validator->IdP($adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_CAT_INST_ID));
94
        } catch(Exception $e) {
95
            return_error(web\lib\admin\API::ERROR_INVALID_PARAMETER, "IdP identifier does not exist!");
96
        }
97
        // here is the token
98
        $mgmt = new core\UserManagement();
99
        $newtoken = $mgmt->createToken(true, $adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_ADMINID), $idp);
0 ignored issues
show
Bug introduced by
It seems like $adminApi->firstParamete...API::AUXATTRIB_ADMINID) can also be of type false; however, parameter $for of core\UserManagement::createToken() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        $newtoken = $mgmt->createToken(true, /** @scrutinizer ignore-type */ $adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_ADMINID), $idp);
Loading history...
100
        $URL = "https://" . $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) . "/action_enrollment.php?token=$newtoken";
101
        $success = ["TOKEN URL" => $URL];
102
        // done with the essentials - display in response. But if we also have an email address, send it there
103
        $email = $adminApi->firstParameterInstance($scrubbedParameters, web\lib\admin\API::AUXATTRIB_ADMINEMAIL);
104
        if ($email !== FALSE) {
105
            $sent = \core\common\OutsideComm::adminInvitationMail($email, "EXISTING-FED", $newtoken, $idp->name, $fed);
106
            $success["EMAIL SENT"] = $sent;
107
        }
108
        return_success($success);
109
        break;
110
    default:
111
        return_error(web\lib\admin\API::ERROR_INVALID_ACTION, "Not implemented yet.");
112
}
113