This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #625 from SUSE/authorization-tokens
Introduce application tokens
- Loading branch information
Showing
29 changed files
with
573 additions
and
3 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,12 @@ | ||
$(document).on "page:change", -> | ||
$('#add_application_token_btn').on 'click', (event) -> | ||
$('#add_application_token_form').toggle 400, "swing", -> | ||
if $('#add_application_token_form').is(':visible') | ||
$('#add_team_btn i').addClass("fa-minus-circle") | ||
$('#add_team_btn i').removeClass("fa-plus-circle") | ||
$('#team_name').focus() | ||
layout_resizer() | ||
else | ||
$('#add_team_btn i').removeClass("fa-minus-circle") | ||
$('#add_team_btn i').addClass("fa-plus-circle") | ||
layout_resizer() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# ApplicationTokensController manages the creation/removal of application tokens | ||
class ApplicationTokensController < ApplicationController | ||
respond_to :js | ||
|
||
# POST /application_tokens | ||
def create | ||
@plain_token = Devise.friendly_token | ||
|
||
@application_token = ApplicationToken.new(create_params) | ||
@application_token.user = current_user | ||
@application_token.token_salt = BCrypt::Engine.generate_salt | ||
@application_token.token_hash = BCrypt::Engine.hash_secret( | ||
@plain_token, | ||
@application_token.token_salt) | ||
|
||
if @application_token.save | ||
@application_token.create_activity!(:create, current_user) | ||
respond_with @application_token | ||
else | ||
respond_with @application_token.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /application_token/1 | ||
def destroy | ||
@application_token = ApplicationToken.find(params[:id]) | ||
@application_token.create_activity!(:destroy, current_user) | ||
@application_token.destroy | ||
|
||
respond_with @application_token | ||
end | ||
|
||
private | ||
|
||
def create_params | ||
permitted = [:application] | ||
params.require(:application_token).permit(permitted) | ||
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,25 @@ | ||
class ApplicationToken < ActiveRecord::Base | ||
include PublicActivity::Common | ||
|
||
belongs_to :user | ||
|
||
validates :application, uniqueness: { scope: "user_id" } | ||
|
||
validate :limit_number_of_tokens_per_user, on: :create | ||
|
||
def limit_number_of_tokens_per_user | ||
max_reached = ApplicationToken.where(user_id: user_id).count >= User::APPLICATION_TOKENS_MAX | ||
errors.add( | ||
:base, | ||
"Users cannot have more than #{User::APPLICATION_TOKENS_MAX} " \ | ||
"application tokens") if max_reached | ||
end | ||
|
||
# Create the activity regarding this application token. | ||
def create_activity!(type, owner) | ||
create_activity( | ||
type, | ||
owner: owner, | ||
parameters: { application: application }) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
tr id="application_token_#{application_token.id}" | ||
td= application_token.application | ||
td | ||
a[class="btn btn-default" | ||
data-placement="left" | ||
data-toggle="popover" | ||
data-title="Please confirm" | ||
data-content='<p>Are you sure you want to remove this token?</p><a class="btn btn-default">No</a> <a class="btn btn-primary" data-method="delete" rel="nofollow" data-remote="true" href="#{url_for(application_token)}">Yes</a>' | ||
data-html="true" | ||
tabindex="0" | ||
role="button" | ||
] | ||
i.fa.fa-trash.fa-lg |
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,13 @@ | ||
<% if @application_token.errors.any? %> | ||
$('#alert p').html("<%= escape_javascript(@application_token.errors.full_messages.join('<br/>')) %>"); | ||
$('#alert').fadeIn(); | ||
<% else %> | ||
$("<%= escape_javascript(render @application_token) %>").appendTo("#application_tokens"); | ||
$('#alert p').html("New token created: <code><%= @plain_token %></code>"); | ||
$('#alert').fadeIn(); | ||
$('#add_application_token_form').fadeOut(); | ||
<% if current_user.application_tokens.count >= User::APPLICATION_TOKENS_MAX %> | ||
$('#add_application_token_btn').attr("disabled", "disabled"); | ||
<% 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,11 @@ | ||
<% if @application_token.errors.any? %> | ||
$('#alert p').html("<%= escape_javascript(@application_token.errors.full_messages.join('<br/>')) %>"); | ||
$('#alert').fadeIn(); | ||
<% else %> | ||
$("#application_token_<%= @application_token.id %>").remove(); | ||
$('#alert p').html("<em>\"<%= @application_token.application %>\"</em> token has been removed"); | ||
$('#alert').fadeIn(); | ||
<% if current_user.application_tokens.count < User::APPLICATION_TOKENS_MAX %> | ||
$('#add_application_token_btn').removeAttr("disabled"); | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
= CSV.generate_line(["application token", "#{activity.parameters[:application]}", "create", "-", activity.owner.username, activity.created_at, "-"]) |
15 changes: 15 additions & 0 deletions
15
app/views/public_activity/application_token/_create.html.slim
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 @@ | ||
li | ||
.activity-type.application-token-created | ||
i.fa.fa-key | ||
.user-image | ||
= user_image_tag(activity.owner.email) | ||
.description | ||
h6 | ||
strong | ||
= activity.owner.username | ||
| created a token for the " | ||
em= activity.parameters[:application] | ||
| " application | ||
small | ||
i.fa.fa-clock-o | ||
= activity_time_tag activity.created_at |
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 @@ | ||
= CSV.generate_line(["application token", "#{activity.parameters[:application]}", "destroy", "-", activity.owner.username, activity.created_at, "-"]) |
15 changes: 15 additions & 0 deletions
15
app/views/public_activity/application_token/_destroy.html.slim
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 @@ | ||
li | ||
.activity-type.application-token-destroyed | ||
i.fa.fa-key | ||
.user-image | ||
= user_image_tag(activity.owner.email) | ||
.description | ||
h6 | ||
strong | ||
= activity.owner.username | ||
| removed the token of " | ||
em= activity.parameters[:application] | ||
| " application | ||
small | ||
i.fa.fa-clock-o | ||
= activity_time_tag activity.created_at |
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,12 @@ | ||
class CreateApplicationTokens < ActiveRecord::Migration | ||
def change | ||
create_table :application_tokens do |t| | ||
t.string :application, null: false | ||
t.string :token_hash, null:false | ||
t.string :token_salt, null:false | ||
t.integer :user_id, null:false | ||
end | ||
|
||
add_index :application_tokens, :user_id | ||
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
Oops, something went wrong.