Reauth endpoint
This commit is contained in:
@@ -33,7 +33,7 @@ class Login extends Api
|
||||
case "NotFound":
|
||||
throw ApiError::notFound("user");
|
||||
case "Invalid":
|
||||
throw ApiError::unauthorized("Invalid username or password");
|
||||
throw ApiError::notAllowed("Invalid username or password");
|
||||
default:
|
||||
throw $err;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ class Posts extends Api
|
||||
$post = Post::getByID($id);
|
||||
|
||||
// Throw 400 if we aren't admin but trying to edit another users post.
|
||||
if (!$self->getIsAdmin() && $post->getUser()->getID() !== $self->getID()) throw ApiError::unauthorized("Not allowed");
|
||||
if (!$self->getIsAdmin() && $post->getUser()->getID() !== $self->getID()) throw ApiError::notAllowed("Not allowed");
|
||||
|
||||
// Try update.
|
||||
Response::json($post->update($content));
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Api\Refresh;
|
||||
|
||||
use Exception;
|
||||
use Khofmann\Api\Api;
|
||||
use Khofmann\ApiError\ApiError;
|
||||
use Khofmann\Input\Input;
|
||||
use Khofmann\Response\Response;
|
||||
use Khofmann\Models\User\User;
|
||||
use Khofmann\Request\Request;
|
||||
|
||||
class Refresh extends Api
|
||||
{
|
||||
public function post(): void
|
||||
{
|
||||
// Fetch all required inputs.
|
||||
// Throw 400 error if a required one is missing.
|
||||
$token = Request::token();
|
||||
$refreshToken = Input::post("refreshToken");
|
||||
if (empty($refreshToken)) throw ApiError::missingField(["refreshToken"]);
|
||||
|
||||
// Try and log in user.
|
||||
// Throw errors according to situation.
|
||||
try {
|
||||
Response::json(User::refresh($token, $refreshToken));
|
||||
} catch (Exception $err) {
|
||||
switch ($err->getMessage()) {
|
||||
case "Failed":
|
||||
throw ApiError::failed("Refresh failed");
|
||||
case "NotFound":
|
||||
throw ApiError::unauthorized("Not authorized");
|
||||
default:
|
||||
throw $err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+51
-2
@@ -310,6 +310,45 @@ paths:
|
||||
value: { "code": "NotFound", "entity": "post" }
|
||||
tags:
|
||||
- Post
|
||||
/refresh:
|
||||
post:
|
||||
summary: Refresh
|
||||
description: Token refresh.
|
||||
security:
|
||||
- BasicAuth: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/RefreshRequest"
|
||||
responses:
|
||||
200:
|
||||
description: Success.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/LoginResponse"
|
||||
400:
|
||||
description: Missing fields.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/MissingFieldResponse"
|
||||
examples:
|
||||
Missing fields:
|
||||
value: { "code": "MissingField", "fields": ["refreshToken"] }
|
||||
500:
|
||||
description: Failed.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/FailedResponse"
|
||||
examples:
|
||||
Failed:
|
||||
value: { "code": "Failed", "message": "Refresh failed" }
|
||||
tags:
|
||||
- Refresh
|
||||
/register:
|
||||
post:
|
||||
summary: Register
|
||||
@@ -678,8 +717,7 @@ components:
|
||||
UnauthorizedResponse:
|
||||
type: object
|
||||
properties:
|
||||
code:
|
||||
type: Unauthorized
|
||||
code: type:NotAllowed
|
||||
message:
|
||||
type: string
|
||||
FailedResponse:
|
||||
@@ -731,6 +769,8 @@ components:
|
||||
$ref: "#/components/schemas/UserResponse"
|
||||
token:
|
||||
type: string
|
||||
refreshToken:
|
||||
type: string
|
||||
UserResponse:
|
||||
type: object
|
||||
properties:
|
||||
@@ -855,6 +895,14 @@ components:
|
||||
properties:
|
||||
content:
|
||||
type: string
|
||||
RefreshRequest:
|
||||
type: object
|
||||
required:
|
||||
- refreshToken
|
||||
properties:
|
||||
refreshToken:
|
||||
type: string
|
||||
format: uuid4
|
||||
securitySchemes:
|
||||
BasicAuth:
|
||||
type: apiKey
|
||||
@@ -864,4 +912,5 @@ tags:
|
||||
- name: Login/Logout
|
||||
- name: Post
|
||||
- name: Register
|
||||
- name: Refresh
|
||||
- name: User
|
||||
|
||||
+18
-10
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user