Passed
Push — master ( bda737...e0ed8e )
by Nils
09:32
created

get_authorization_header()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 18
rs 9.6111
1
<?php
2
3
function generate_jwt($headers, $payload) {
4
	$headers_encoded = base64url_encode(json_encode($headers));
5
	
6
	$payload_encoded = base64url_encode(json_encode($payload));
7
	
8
	$signature = hash_hmac('SHA256', "$headers_encoded.$payload_encoded", DB_PASSWD, true);
9
	$signature_encoded = base64url_encode($signature);
10
	
11
	$jwt = "$headers_encoded.$payload_encoded.$signature_encoded";
12
	
13
	return $jwt;
14
}
15
16
function is_jwt_valid($jwt) {
17
	// split the jwt
18
	$tokenParts = explode('.', $jwt);
19
	$header = base64_decode($tokenParts[0]);
20
	$payload = base64_decode($tokenParts[1]);
21
	$signature_provided = $tokenParts[2];
22
23
	// check the expiration time - note this will cause an error if there is no 'exp' claim in the jwt
24
	$expiration = json_decode($payload)->exp;
25
	$is_token_expired = ($expiration - time()) < 0;
26
27
	// build a signature based on the header and payload using the secret
28
	$base64_url_header = base64url_encode($header);
29
	$base64_url_payload = base64url_encode($payload);
30
	$signature = hash_hmac('SHA256', $base64_url_header . "." . $base64_url_payload, DB_PASSWD, true);
31
	$base64_url_signature = base64url_encode($signature);
32
33
	// verify it matches the signature provided in the jwt
34
	$is_signature_valid = ($base64_url_signature === $signature_provided);
35
	
36
	if ($is_token_expired || !$is_signature_valid) {
37
		return FALSE;
38
	} else {
39
		return TRUE;
40
	}
41
}
42
43
function base64url_encode($data) {
44
    return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
45
}
46
47
function get_authorization_header(){
48
	$headers = null;
49
	
50
	if (isset($_SERVER['Authorization'])) {
51
		$headers = trim($_SERVER["Authorization"]);
52
	} else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI
53
		$headers = trim($_SERVER["HTTP_AUTHORIZATION"]);
54
	} else if (function_exists('apache_request_headers')) {
55
		$requestHeaders = apache_request_headers();
56
		// Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization)
57
		$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
0 ignored issues
show
Bug introduced by
It seems like $requestHeaders can also be of type true; however, parameter $array of array_keys() does only seem to accept array, 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

57
		$requestHeaders = array_combine(array_map('ucwords', array_keys(/** @scrutinizer ignore-type */ $requestHeaders)), array_values($requestHeaders));
Loading history...
Bug introduced by
It seems like $requestHeaders can also be of type true; however, parameter $array of array_values() does only seem to accept array, 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

57
		$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values(/** @scrutinizer ignore-type */ $requestHeaders));
Loading history...
58
		//print_r($requestHeaders);
59
		if (isset($requestHeaders['Authorization'])) {
60
			$headers = trim($requestHeaders['Authorization']);
61
		}
62
	}
63
	
64
	return $headers;
65
}
66
67
function get_bearer_token() {
68
    $headers = get_authorization_header();
69
	
70
    // HEADER: Get the access token from the header
71
    if (!empty($headers)) {
72
        if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
73
            return $matches[1];
74
        }
75
    }
76
    return null;
77
}