forked from SUSE/Portus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This new endpoint lives inside of the user namespace and its main goal is to allow people to create the first admin user from the API. This endpoint is only allowed if the `first_user_admin` option has not been disabled. Another restriction is that there shouldn't be a user already created. Last but not least, this method can be called without being authenticated. In the ideal case, you would call this method when you just started your Portus instance. Then, you will get the first user created (same arguments as `POST /api/v1/users`) and it will also get an application token assigned to it. The reponse will be the `plain_token` from the application token. Thus, the whole point of this method is to follow this workflow: 1. Portus instance deployed. 2. Admin uses this bootstrap endpoint to create the admin user and get an application token. 3. With this application token the admin starts to administrate the instance (e.g. register the registry on Portus) entirely from the API. 4. Admin makes the Portus instance available inside of the organization and starts structuring it inside of Portus. Finally, this commit also started to make error responses more uniform. This was firstly done to DRY some of the code created by this feature. It probably needs more work (see SUSE#1437). See SUSE#1412 Signed-off-by: Miquel Sabaté Solà <[email protected]>
- Loading branch information
Showing
11 changed files
with
387 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module API | ||
module Helpers | ||
# Helpers regarding the management of authentication tokens. This module | ||
# mostly contains methods that are shared across different paths. | ||
module ApplicationTokens | ||
# Create an application token for the given user with the given | ||
# ID. The `params` parameter contains the parameters to be passed to the | ||
# `ApplicationToken.create_token` method as `params`. | ||
# | ||
# This method already sends the proper HTTP response and code. | ||
def create_application_token!(user, id, params) | ||
if user.valid? | ||
application_token, plain_token = ApplicationToken.create_token( | ||
current_user: user, | ||
user_id: id, | ||
params: params | ||
) | ||
|
||
if application_token.errors.empty? | ||
status 201 | ||
{ plain_token: plain_token } | ||
else | ||
bad_request!(application_token.errors) | ||
end | ||
else | ||
bad_request!(user.errors) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
# frozen_string_literal: true | ||
|
||
require "portus/auth_from_token" | ||
|
||
module API | ||
module Helpers | ||
# Errors implements helper methods for API error responses. | ||
module Errors | ||
def api_error!(code:, messages:) | ||
obj = messages.is_a?(String) ? [messages] : messages | ||
error!(obj, code) | ||
end | ||
|
||
# Sends a `400 Bad Request` error with a possible message as the response | ||
# body. | ||
def bad_request!(msg = "Bad Request") | ||
api_error!(code: 400, messages: msg) | ||
end | ||
|
||
# Sends a `401 Unauthorized` error with a possible message as the response | ||
# body. | ||
def unauthorized!(msg = "Unauthorized") | ||
api_error!(code: 401, messages: msg) | ||
end | ||
|
||
# Sends a `403 Forbidden` error with a possible message as the response | ||
# body. | ||
def forbidden!(msg = "Forbidden") | ||
api_error!(code: 403, messages: msg) | ||
end | ||
|
||
# Sends a `404 Not found` error with a possible message as the response | ||
# body. | ||
def not_found!(msg = "Not found") | ||
api_error!(code: 404, messages: msg) | ||
end | ||
|
||
# Sends a `405 Method Not Allowed` error with a possible message as the | ||
# response body. | ||
def method_not_allowed!(msg = "Method Not Allowed") | ||
api_error!(code: 405, messages: msg) | ||
end | ||
|
||
# Sends a `422 Unprocessable Entity` error with a possible message as the | ||
# response body. | ||
def unprocessable_entity!(msg = "Unprocessable Entity") | ||
api_error!(code: 422, messages: msg) | ||
end | ||
|
||
# Sends a `405 Internal Server Error` error with a possible message as the | ||
# response body. | ||
def internal_server_error!(msg = "Internal Server Error") | ||
api_error!(code: 500, messages: msg) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module API | ||
module Helpers | ||
# Helpers for namespaces | ||
module Namespaces | ||
# Returns an aggregate of the accessible namespaces for the current user. | ||
def accessible_namespaces | ||
special = Namespace.special_for(current_user).order(created_at: :asc) | ||
normal = policy_scope(Namespace).order(created_at: :asc) | ||
special + normal | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.