FOSSology  4.7.1
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  $date = DateTime::createFromFormat("!Y-m-d", $tokenExpire);
112  $errors = DateTime::getLastErrors();
113  if ($date === false ||
114  ($errors !== false &&
115  ($errors['warning_count'] > 0 || $errors['error_count'] > 0)) ||
116  $date->format('Y-m-d') !== $tokenExpire) {
117  throw new HttpBadRequestException("Invalid date format provided");
118  }
119  $this->tokenExpire = $date;
120  return $this;
121  }
122 
127  public function setUsername(string $username): TokenRequest
128  {
129  $this->username = $username;
130  return $this;
131  }
132 
137  public function setPassword(string $password): TokenRequest
138  {
139  $this->password = $password;
140  return $this;
141  }
142 
146  public function getTokenName(): string
147  {
148  return $this->tokenName;
149  }
150 
154  public function getTokenScope(): string
155  {
156  return $this->tokenScope;
157  }
158 
162  public function getTokenExpire(): string
163  {
164  return $this->tokenExpire->format('Y-m-d');
165  }
166 
170  public function getUsername(): string
171  {
172  return $this->username;
173  }
174 
178  public function getPassword(): string
179  {
180  return $this->password;
181  }
182 
189  public static function fromArray(array $input, int $version): TokenRequest
190  {
191  if (! array_key_exists("username", $input)) {
192  $input["username"] = "";
193  }
194  if (! array_key_exists("password", $input)) {
195  $input["password"] = "";
196  }
197  if ($version == ApiVersion::V1) {
198  if (! ArrayOperation::arrayKeysExists($input, self::VERSION_1_KEYS)) {
199  throw new HttpBadRequestException("Not all required parameters sent.");
200  }
201  return new TokenRequest(
202  $input["token_name"],
203  $input["token_scope"],
204  $input["token_expire"],
205  $input["username"],
206  $input["password"]
207  );
208  } else {
209  if (! ArrayOperation::arrayKeysExists($input, self::VERSION_2_KEYS)) {
210  throw new HttpBadRequestException("Not all required parameters sent.");
211  }
212  return new TokenRequest(
213  $input["tokenName"],
214  $input["tokenScope"],
215  $input["tokenExpire"],
216  $input["username"],
217  $input["password"]
218  );
219  }
220  }
221 }
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="")