FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TokenRequest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2023 Siemens AG
4  SPDX-FileContributor: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7  */
8 
9 namespace Fossology\UI\Api\Models;
10 
11 use DateTime;
15 
17 {
22  const VERSION_1_KEYS = ["username", "password", "token_name", "token_scope",
23  "token_expire"];
28  const VERSION_2_KEYS = ["username", "password", "tokenName", "tokenScope",
29  "tokenExpire"];
34  private $tokenName;
39  private $tokenScope;
44  private $tokenExpire;
49  private $username;
54  private $password;
55 
64  public function __construct(string $tokenName, string $tokenScope,
65  string $tokenExpire, string $username="",
66  string $password="")
67  {
68  $this->setTokenName($tokenName);
69  $this->setTokenScope($tokenScope);
70  $this->setTokenExpire($tokenExpire);
71  $this->setUsername($username);
72  $this->setPassword($password);
73  }
74 
80  public function setTokenName(string $tokenName): TokenRequest
81  {
82  if (empty($tokenName)) {
83  throw new HttpBadRequestException("Token name cannot be empty");
84  }
85  $this->tokenName = $tokenName;
86  return $this;
87  }
88 
94  public function setTokenScope(string $tokenScope): TokenRequest
95  {
96  $tokenScope = strtolower($tokenScope);
97  if (!in_array($tokenScope, RestHelper::VALID_SCOPES)) {
98  throw new HttpBadRequestException("Invalid scope provided");
99  }
100  $this->tokenScope = RestHelper::SCOPE_DB_MAP[$tokenScope];
101  return $this;
102  }
103 
109  public function setTokenExpire(string $tokenExpire): TokenRequest
110  {
111  $this->tokenExpire = DateTime::createFromFormat("Y-m-d", $tokenExpire);
112  if ($this->tokenExpire === false) {
113  throw new HttpBadRequestException("Invalid date format provided");
114  }
115  return $this;
116  }
117 
122  public function setUsername(string $username): TokenRequest
123  {
124  $this->username = $username;
125  return $this;
126  }
127 
132  public function setPassword(string $password): TokenRequest
133  {
134  $this->password = $password;
135  return $this;
136  }
137 
141  public function getTokenName(): string
142  {
143  return $this->tokenName;
144  }
145 
149  public function getTokenScope(): string
150  {
151  return $this->tokenScope;
152  }
153 
157  public function getTokenExpire(): string
158  {
159  return $this->tokenExpire->format('Y-m-d');
160  }
161 
165  public function getUsername(): string
166  {
167  return $this->username;
168  }
169 
173  public function getPassword(): string
174  {
175  return $this->password;
176  }
177 
184  public static function fromArray(array $input, int $version): TokenRequest
185  {
186  if (! array_key_exists("username", $input)) {
187  $input["username"] = "";
188  }
189  if (! array_key_exists("password", $input)) {
190  $input["password"] = "";
191  }
192  if ($version == ApiVersion::V1) {
193  if (! ArrayOperation::arrayKeysExists($input, self::VERSION_1_KEYS)) {
194  throw new HttpBadRequestException("Not all required parameters sent.");
195  }
196  return new TokenRequest(
197  $input["token_name"],
198  $input["token_scope"],
199  $input["token_expire"],
200  $input["username"],
201  $input["password"]
202  );
203  } else {
204  if (! ArrayOperation::arrayKeysExists($input, self::VERSION_2_KEYS)) {
205  throw new HttpBadRequestException("Not all required parameters sent.");
206  }
207  return new TokenRequest(
208  $input["tokenName"],
209  $input["tokenScope"],
210  $input["tokenExpire"],
211  $input["username"],
212  $input["password"]
213  );
214  }
215  }
216 }
static arrayKeysExists(array $array, array $keys)
Check if a list of keys exists in associative array.
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32
static fromArray(array $input, int $version)
__construct(string $tokenName, string $tokenScope, string $tokenExpire, string $username="", string $password="")