Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Change authorized applications page (mastodon#17656)
Browse files Browse the repository at this point in the history
* Change authorized applications page

* Hide revoke button for superapps and suspended accounts

* Clean up db/schema.rb
  • Loading branch information
Gargron authored Mar 1, 2022
1 parent 233f7e6 commit 50ea54b
Show file tree
Hide file tree
Showing 20 changed files with 393 additions and 62 deletions.
1 change: 1 addition & 0 deletions app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Api::BaseController < ApplicationController
DEFAULT_ACCOUNTS_LIMIT = 40

include RateLimitHeaders
include AccessTokenTrackingConcern

skip_before_action :store_current_location
skip_before_action :require_functional!, unless: :whitelist_mode?
Expand Down
21 changes: 21 additions & 0 deletions app/controllers/concerns/access_token_tracking_concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module AccessTokenTrackingConcern
extend ActiveSupport::Concern

ACCESS_TOKEN_UPDATE_FREQUENCY = 24.hours.freeze

included do
before_action :update_access_token_last_used
end

private

def update_access_token_last_used
doorkeeper_token.update_last_used(request) if access_token_needs_update?
end

def access_token_needs_update?
doorkeeper_token.present? && (doorkeeper_token.last_used_at.nil? || doorkeeper_token.last_used_at < ACCESS_TOKEN_UPDATE_FREQUENCY.ago)
end
end
4 changes: 2 additions & 2 deletions app/controllers/concerns/session_tracking_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module SessionTrackingConcern
extend ActiveSupport::Concern

UPDATE_SIGN_IN_HOURS = 24
SESSION_UPDATE_FREQUENCY = 24.hours.freeze

included do
before_action :set_session_activity
Expand All @@ -17,6 +17,6 @@ def set_session_activity
end

def session_needs_update?
!current_session.nil? && current_session.updated_at < UPDATE_SIGN_IN_HOURS.hours.ago
!current_session.nil? && current_session.updated_at < SESSION_UPDATE_FREQUENCY.ago
end
end
4 changes: 2 additions & 2 deletions app/controllers/concerns/user_tracking_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module UserTrackingConcern
extend ActiveSupport::Concern

UPDATE_SIGN_IN_FREQUENCY = 24.hours.freeze
SIGN_IN_UPDATE_FREQUENCY = 24.hours.freeze

included do
before_action :update_user_sign_in
Expand All @@ -16,6 +16,6 @@ def update_user_sign_in
end

def user_needs_sign_in_update?
user_signed_in? && (current_user.current_sign_in_at.nil? || current_user.current_sign_in_at < UPDATE_SIGN_IN_FREQUENCY.ago)
user_signed_in? && (current_user.current_sign_in_at.nil? || current_user.current_sign_in_at < SIGN_IN_UPDATE_FREQUENCY.ago)
end
end
15 changes: 15 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,19 @@ def render_initial_state
content_tag(:script, json_escape(json).html_safe, id: 'initial-state', type: 'application/json')
# rubocop:enable Rails/OutputSafety
end

def grouped_scopes(scopes)
scope_parser = ScopeParser.new
scope_transformer = ScopeTransformer.new

scopes.each_with_object({}) do |str, h|
scope = scope_transformer.apply(scope_parser.parse(str))

if h[scope.key]
h[scope.key].merge!(scope)
else
h[scope.key] = scope
end
end.values
end
end
10 changes: 10 additions & 0 deletions app/javascript/styles/mastodon/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,12 @@ a.name-tag,
text-decoration: none;
margin-bottom: 10px;

.account-role {
vertical-align: middle;
}
}

a.announcements-list__item__title {
&:hover,
&:focus,
&:active {
Expand All @@ -925,6 +931,10 @@ a.name-tag,
align-items: center;
}

&__permissions {
margin-top: 10px;
}

&:last-child {
border-bottom: 0;
}
Expand Down
13 changes: 5 additions & 8 deletions app/javascript/styles/mastodon/containers.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.container-alt {
width: 700px;
margin: 0 auto;
margin-top: 40px;

@media screen and (max-width: 740px) {
width: 100%;
Expand Down Expand Up @@ -67,22 +66,20 @@
line-height: 18px;
box-sizing: border-box;
padding: 20px 0;
padding-bottom: 0;
margin-bottom: -30px;
margin-top: 40px;
margin-bottom: 10px;
border-bottom: 1px solid $ui-base-color;

@media screen and (max-width: 440px) {
width: 100%;
margin: 0;
margin-bottom: 10px;
padding: 20px;
padding-bottom: 0;
}

.avatar {
width: 40px;
height: 40px;
margin-right: 8px;
margin-right: 10px;

img {
width: 100%;
Expand All @@ -96,7 +93,7 @@
.name {
flex: 1 1 auto;
color: $secondary-text-color;
width: calc(100% - 88px);
width: calc(100% - 90px);

.username {
display: block;
Expand All @@ -110,7 +107,7 @@
display: block;
font-size: 32px;
line-height: 40px;
margin-left: 8px;
margin-left: 10px;
}
}

Expand Down
71 changes: 69 additions & 2 deletions app/javascript/styles/mastodon/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,41 @@ code {
}
}
}
}

@media screen and (max-width: 740px) and (min-width: 441px) {
margin-top: 40px;
.oauth-prompt {
h3 {
color: $ui-secondary-color;
font-size: 17px;
line-height: 22px;
font-weight: 500;
margin-bottom: 30px;
}

p {
font-size: 14px;
line-height: 18px;
margin-bottom: 30px;
}

.permissions-list {
border: 1px solid $ui-base-color;
border-radius: 4px;
background: darken($ui-base-color, 4%);
margin-bottom: 30px;
}

.actions {
margin: 0 -10px;
display: flex;

form {
box-sizing: border-box;
padding: 0 10px;
flex: 1 1 auto;
min-height: 1px;
width: 50%;
}
}
}

Expand Down Expand Up @@ -1005,3 +1037,38 @@ code {
display: none;
}
}

.permissions-list {
&__item {
padding: 15px;
color: $ui-secondary-color;
border-bottom: 1px solid lighten($ui-base-color, 4%);
display: flex;
align-items: center;

&__text {
flex: 1 1 auto;

&__title {
font-weight: 500;
}

&__type {
color: $darker-text-color;
}
}

&__icon {
flex: 0 0 auto;
font-size: 18px;
width: 30px;
color: $valid-value-color;
display: flex;
align-items: center;
}

&:last-child {
border-bottom: 0;
}
}
}
4 changes: 4 additions & 0 deletions app/lib/access_token_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def revoke(clock = Time)
update(revoked_at: clock.now.utc)
end

def update_last_used(request, clock = Time)
update(last_used_at: clock.now.utc, last_used_ip: request.remote_ip)
end

def push_to_streaming_api
Redis.current.publish("timeline:access_token:#{id}", Oj.dump(event: :kill)) if revoked? || destroyed?
end
Expand Down
4 changes: 4 additions & 0 deletions app/lib/application_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ module ApplicationExtension
validates :website, url: true, length: { maximum: 2_000 }, if: :website?
validates :redirect_uri, length: { maximum: 2_000 }
end

def most_recently_used_access_token
@most_recently_used_access_token ||= access_tokens.where.not(last_used_at: nil).order(last_used_at: :desc).first
end
end
10 changes: 10 additions & 0 deletions app/lib/scope_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class ScopeParser < Parslet::Parser
rule(:term) { match('[a-z]').repeat(1).as(:term) }
rule(:colon) { str(':') }
rule(:access) { (str('write') | str('read')).as(:access) }
rule(:namespace) { str('admin').as(:namespace) }
rule(:scope) { ((namespace >> colon).maybe >> ((access >> colon >> term) | access | term)).as(:scope) }
root(:scope)
end
40 changes: 40 additions & 0 deletions app/lib/scope_transformer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

class ScopeTransformer < Parslet::Transform
class Scope
DEFAULT_TERM = 'all'
DEFAULT_ACCESS = %w(read write).freeze

attr_reader :namespace, :term

def initialize(scope)
@namespace = scope[:namespace]&.to_s
@access = scope[:access] ? [scope[:access].to_s] : DEFAULT_ACCESS.dup
@term = scope[:term]&.to_s || DEFAULT_TERM
end

def key
@key ||= [@namespace, @term].compact.join('/')
end

def access
@access.join('/')
end

def merge(other_scope)
clone.merge!(other_scope)
end

def merge!(other_scope)
raise ArgumentError unless other_scope.namespace == namespace && other_scope.term == term

@access.concat(other_scope.instance_variable_get('@access'))
@access.uniq!
@access.sort!

self
end
end

rule(scope: subtree(:scope)) { Scope.new(scope) }
end
3 changes: 2 additions & 1 deletion app/views/layouts/modal.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
= fa_icon 'sign-out'

.container-alt= yield

.modal-layout__mastodon
%div
%img{alt:'', draggable:'false', src:"#{mascot_url}"}
%img{alt: '', draggable: 'false', src: mascot_url }

= render template: 'layouts/application'
50 changes: 31 additions & 19 deletions app/views/oauth/authorizations/new.html.haml
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
- content_for :page_title do
= t('doorkeeper.authorizations.new.title')

.form-container
.form-container.simple_form
.oauth-prompt
%h2= t('doorkeeper.authorizations.new.prompt', client_name: @pre_auth.client.name)
%h3= t('doorkeeper.authorizations.new.title')

%p
= t('doorkeeper.authorizations.new.able_to')
!= @pre_auth.scopes.map { |scope| t(scope, scope: [:doorkeeper, :scopes]) }.map { |s| "<strong>#{s}</strong>" }.to_sentence
%p= t('doorkeeper.authorizations.new.prompt_html', client_name: content_tag(:strong, @pre_auth.client.name))

= form_tag oauth_authorization_path, method: :post, class: 'simple_form' do
= hidden_field_tag :client_id, @pre_auth.client.uid
= hidden_field_tag :redirect_uri, @pre_auth.redirect_uri
= hidden_field_tag :state, @pre_auth.state
= hidden_field_tag :response_type, @pre_auth.response_type
= hidden_field_tag :scope, @pre_auth.scope
= button_tag t('doorkeeper.authorizations.buttons.authorize'), type: :submit
%h3= t('doorkeeper.authorizations.new.review_permissions')

= form_tag oauth_authorization_path, method: :delete, class: 'simple_form' do
= hidden_field_tag :client_id, @pre_auth.client.uid
= hidden_field_tag :redirect_uri, @pre_auth.redirect_uri
= hidden_field_tag :state, @pre_auth.state
= hidden_field_tag :response_type, @pre_auth.response_type
= hidden_field_tag :scope, @pre_auth.scope
= button_tag t('doorkeeper.authorizations.buttons.deny'), type: :submit, class: 'negative'
%ul.permissions-list
- grouped_scopes(@pre_auth.scopes).each do |scope|
%li.permissions-list__item
.permissions-list__item__icon
= fa_icon('check')
.permissions-list__item__text
.permissions-list__item__text__title
= t(scope.key, scope: [:doorkeeper, :grouped_scopes, :title])
.permissions-list__item__text__type
= t(scope.access, scope: [:doorkeeper, :grouped_scopes, :access])

.actions
= form_tag oauth_authorization_path, method: :post do
= hidden_field_tag :client_id, @pre_auth.client.uid
= hidden_field_tag :redirect_uri, @pre_auth.redirect_uri
= hidden_field_tag :state, @pre_auth.state
= hidden_field_tag :response_type, @pre_auth.response_type
= hidden_field_tag :scope, @pre_auth.scope
= button_tag t('doorkeeper.authorizations.buttons.authorize'), type: :submit

= form_tag oauth_authorization_path, method: :delete do
= hidden_field_tag :client_id, @pre_auth.client.uid
= hidden_field_tag :redirect_uri, @pre_auth.redirect_uri
= hidden_field_tag :state, @pre_auth.state
= hidden_field_tag :response_type, @pre_auth.response_type
= hidden_field_tag :scope, @pre_auth.scope
= button_tag t('doorkeeper.authorizations.buttons.deny'), type: :submit, class: 'negative'
Loading

0 comments on commit 50ea54b

Please sign in to comment.