PluginProbe
ManageWP Worker / 3.9.22
ManageWP Worker v3.9.22
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / lib / google-api-client / auth / Google_AssertionCredentials.php

Google_AssertionCredentials.php in ManageWP Worker 3.9.22, at lib/google-api-client/auth/Google_AssertionCredentials.php

96 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2012 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /**
19 * Credentials object used for OAuth 2.0 Signed JWT assertion grants.
20 *
21 * @author Chirag Shah <[email protected]>
22 */
23 class Google_AssertionCredentials {
24 const MAX_TOKEN_LIFETIME_SECS = 3600;
25
26 public $serviceAccountName;
27 public $scopes;
28 public $privateKey;
29 public $privateKeyPassword;
30 public $assertionType;
31 public $prn;
32
33 /**
34 * @param $serviceAccountName
35 * @param $scopes array List of scopes
36 * @param $privateKey
37 * @param string $privateKeyPassword
38 * @param string $assertionType
39 * @param bool|string $prn The email address of the user for which the
40 * application is requesting delegated access.
41 */
42 public function __construct(
43 $serviceAccountName,
44 $scopes,
45 $privateKey,
46 $privateKeyPassword = 'notasecret',
47 $assertionType = 'http://oauth.net/grant_type/jwt/1.0/bearer',
48 $prn = false) {
49 $this->serviceAccountName = $serviceAccountName;
50 $this->scopes = is_string($scopes) ? $scopes : implode(' ', $scopes);
51 $this->privateKey = $privateKey;
52 $this->privateKeyPassword = $privateKeyPassword;
53 $this->assertionType = $assertionType;
54 $this->prn = $prn;
55 }
56
57 public function generateAssertion() {
58 $now = time();
59
60 $jwtParams = array(
61 'aud' => Google_OAuth2::OAUTH2_TOKEN_URI,
62 'scope' => $this->scopes,
63 'iat' => $now,
64 'exp' => $now + self::MAX_TOKEN_LIFETIME_SECS,
65 'iss' => $this->serviceAccountName,
66 );
67
68 if ($this->prn !== false) {
69 $jwtParams['prn'] = $this->prn;
70 }
71
72 return $this->makeSignedJwt($jwtParams);
73 }
74
75 /**
76 * Creates a signed JWT.
77 * @param array $payload
78 * @return string The signed JWT.
79 */
80 private function makeSignedJwt($payload) {
81 $header = array('typ' => 'JWT', 'alg' => 'RS256');
82
83 $segments = array(
84 Google_Utils::urlSafeB64Encode(json_encode($header)),
85 Google_Utils::urlSafeB64Encode(json_encode($payload))
86 );
87
88 $signingInput = implode('.', $segments);
89 $signer = new Google_P12Signer($this->privateKey, $this->privateKeyPassword);
90 $signature = $signer->sign($signingInput);
91 $segments[] = Google_Utils::urlSafeB64Encode($signature);
92
93 return implode(".", $segments);
94 }
95 }
96