-
-
Notifications
You must be signed in to change notification settings - Fork 117
Add API authentication using OAuth2 Client Credentials flow #3951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
84f1ea9
Add doorkeeper and run installer
Floppy 1c02597
lint doorkeeper generated files
Floppy 7a2543c
link doorkeeper tables to user model
Floppy 31f00d7
run doorkeeper migration
Floppy c1f0cb6
add user and scope config to doorkeeper config
Floppy b941588
applications are owned by users
Floppy c6a90fd
doorkeeper configuration for client credntials flow only
Floppy 8a87f56
authenticate app owner with token
Floppy 80e5db7
custom controller for creating oauth applications
Floppy 3065c1e
add delete link for applications
Floppy 6abd864
show a few more details about apps in list
Floppy 22ccf06
autofocus name field when creating tokens
Floppy 4023e42
generate access tokens
Floppy 01aca64
layout and translations for oauth apps
Floppy 8bcbb15
link oauth apps from admin settings
Floppy 14b60da
remove unnecessary inverse_of
Floppy 1a04a7f
add authentication details to API docs
Floppy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -183,3 +183,5 @@ gem "rswag", "~> 2.16" | |
| gem "warning", "~> 1.5" | ||
|
|
||
| gem "rack-cors", "~> 2.0" | ||
|
|
||
| gem "doorkeeper", "~> 5.8" | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,84 @@ | ||
| class DoorkeeperApplicationsController < ApplicationController | ||
| before_action :get_application, except: [:index, :new, :create] | ||
|
|
||
| def index | ||
| @applications = policy_scope(Doorkeeper::Application) | ||
| end | ||
|
|
||
| def show | ||
| get_access_token | ||
| end | ||
|
|
||
| def new | ||
| authorize Doorkeeper::Application | ||
| @application = Doorkeeper::Application.new( | ||
| redirect_uri: "urn:ietf:wg:oauth:2.0:oob", | ||
| scopes: Doorkeeper.configuration.default_scopes | ||
| ) | ||
| end | ||
|
|
||
| def edit | ||
| end | ||
|
|
||
| def create | ||
| authorize Doorkeeper::Application | ||
| @application = Doorkeeper::Application.create(application_params.merge(owner: current_user)) | ||
| if @application.valid? | ||
| redirect_to @application, notice: t(".success") | ||
| else | ||
| flash.now[:alert] = t(".failure") | ||
| render :new | ||
| end | ||
| end | ||
|
|
||
| def update | ||
| generate_token if application_params[:generate_token] | ||
| @application.update(application_params.except(:generate_token)) | ||
| if @application.save | ||
| get_access_token | ||
| render :show, notice: t(".success") | ||
| else | ||
| flash.now[:alert] = t(".failure") | ||
| render :edit | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| @application.destroy | ||
| redirect_to doorkeeper_applications_path, notice: t(".success") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def application_params | ||
| params.require(:doorkeeper_application).permit( | ||
| :name, | ||
| :redirect_uri, | ||
| :confidential, | ||
| :scopes, | ||
| :generate_token | ||
| ) | ||
| end | ||
|
|
||
| def get_application | ||
| @application = policy_scope(Doorkeeper::Application).find(params[:id]) | ||
| authorize @application | ||
| end | ||
|
|
||
| def generate_token | ||
| # Revoke existing tokens | ||
| Doorkeeper::Application.revoke_tokens_and_grants_for(@application, @application.owner) | ||
| # Create new access token | ||
| token = @application.access_tokens.create( | ||
| expires_in: 6.months, | ||
| resource_owner_id: @application.owner.id, | ||
| scopes: @application.scopes | ||
| ) | ||
| # Make plaintext available to view | ||
| @plaintext_token = token.plaintext_token | ||
| end | ||
|
|
||
| def get_access_token | ||
| @access_token = @application.access_tokens.where(revoked_at: nil).first | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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,48 @@ | ||
| class Doorkeeper::ApplicationPolicy < ApplicationPolicy | ||
| def index? | ||
| user.present? | ||
| end | ||
|
|
||
| def show? | ||
| one_of( | ||
| record.owner == user, | ||
| user&.is_moderator? | ||
| ) | ||
| end | ||
|
|
||
| def create? | ||
| none_of( | ||
| SiteSettings.demo_mode_enabled? | ||
| ) | ||
| end | ||
|
|
||
| def update? | ||
| all_of( | ||
| one_of( | ||
| user == record, | ||
| user&.is_administrator? | ||
| ), | ||
| SiteSettings.multiuser_enabled?, | ||
| none_of( | ||
| SiteSettings.demo_mode_enabled? | ||
| ) | ||
| ) | ||
| end | ||
|
|
||
| def destroy? | ||
| update? | ||
| end | ||
|
|
||
| class Scope | ||
| attr_reader :user, :scope | ||
|
|
||
| def initialize(user, scope) | ||
| @user = user | ||
| @scope = scope | ||
| end | ||
|
|
||
| def resolve | ||
| user.is_moderator? ? scope : scope.where(owner: user) | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,12 @@ | ||
| <% content_for :breadcrumbs do %> | ||
| <nav aria-label="Breadcrumb" class="container-fluid"> | ||
| <ol class="breadcrumb"> | ||
| <li class="breadcrumb-item"><%= link_to t("doorkeeper_applications.index.title"), doorkeeper_applications_path %></li> | ||
| <% if @application&.persisted? %> | ||
| <li class="breadcrumb-item"> | ||
| <%= link_to @application.name, @application %> | ||
| </li> | ||
| <% end %> | ||
| </ol> | ||
| </nav> | ||
| <% end %> |
This file contains hidden or 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,7 @@ | ||
| <%= form_with model: @application do |form| %> | ||
| <%= text_input_row form, :name, autofocus: true %> | ||
| <%= text_input_row form, :redirect_uri, help: t(".redirect_uri.help") %> | ||
| <%= text_input_row form, :scopes, help: t(".scopes.help") %> | ||
| <%= checkbox_input_row form, :confidential, help: t(".confidential.help") %> | ||
| <%= form.submit translate(".submit"), class: "btn btn-primary" %> | ||
| <% end %> |
This file contains hidden or 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,4 @@ | ||
| <%= render "breadcrumb" %> | ||
| <h1><%= t(".title") %></h1> | ||
|
|
||
| <%= render "form" %> |
This file contains hidden or 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,22 @@ | ||
| <h1><%= t(".title") %></h1> | ||
|
|
||
| <p class="lead"><%= t(".description") %></p> | ||
|
|
||
| <table class="table table-striped"> | ||
| <tr> | ||
| <td><%= Doorkeeper::Application.human_attribute_name(:name) %></td> | ||
| <%= content_tag(:td) { Doorkeeper::Application.human_attribute_name(:owner) } if current_user.is_moderator? %> | ||
| <td><%= t Doorkeeper::Application.human_attribute_name(:scopes) %></td> | ||
| <td><%= t Doorkeeper::Application.human_attribute_name(:created_at) %></td> | ||
| </tr> | ||
| <% @applications.find_each do |app| %> | ||
| <tr> | ||
| <td><%= link_to app.name, app %></td> | ||
| <%= content_tag(:td) { app.owner.username } if current_user.is_moderator? %> | ||
| <td><%= app.scopes %></td> | ||
| <td><%= app.created_at.to_fs(:long) %></td> | ||
| </tr> | ||
| <% end %> | ||
| </table> | ||
|
|
||
| <%= link_to t(".new"), new_doorkeeper_application_path, class: "btn btn-primary" %> |
This file contains hidden or 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,4 @@ | ||
| <%= render "breadcrumb" %> | ||
| <h1><%= t(".title") %></h1> | ||
|
|
||
| <%= render "form" %> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.