Commit c769cbb
Changed files (4)
app
controllers
models
app/controllers/oauths_controller.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class OauthsController < ApplicationController
- VALID_RESPONSE_TYPES = [ 'code', 'token' ]
+ VALID_RESPONSE_TYPES = %w[code token].freeze
def show
@client = Client.find_by!(uuid: params[:client_id])
@@ -13,7 +13,7 @@ class OauthsController < ApplicationController
)
end
- if !VALID_RESPONSE_TYPES.include?(params[:response_type])
+ unless VALID_RESPONSE_TYPES.include?(params[:response_type])
return redirect_to @client.redirect_uri_path(
error: 'unsupported_response_type',
state: params[:state]
@@ -33,13 +33,15 @@ class OauthsController < ApplicationController
client = Client.find_by!(uuid: session[:oauth][:client_id])
authorization = client.authorizations.create!(user: current_user)
- if 'code' == session[:oauth][:response_type]
+ if session[:oauth][:response_type] == 'code'
redirect_to client.redirect_uri_path(
code: authorization.code,
state: session[:oauth][:state]
)
- elsif 'token' == session[:oauth][:response_type]
- @access_token = authorization.issue_tokens_to(client, token_type: :access)
+ elsif session[:oauth][:response_type] == 'token'
+ @access_token, = authorization.issue_tokens_to(
+ client, token_types: [:access]
+ )
redirect_to client.redirect_uri_path(
access_token: @access_token.to_jwt,
app/models/token.rb
@@ -43,18 +43,11 @@ class Token < ApplicationRecord
@to_jwt ||= BearerToken.new.encode(claims(custom_claims))
end
- def issue_tokens_to(client, token_type: :all)
+ def issue_tokens_to(client, token_types: [:access, :refresh])
transaction do
revoke!
- if token_type == :all
- [
- Token.create!(subject: subject, audience: client, token_type: :access),
- Token.create!(subject: subject, audience: client, token_type: :refresh),
- ]
- elsif token_type == :access
- Token.create!(subject: subject, audience: client, token_type: :access)
- elsif token_type == :refresh
- Token.create!(subject: subject, audience: client, token_type: :refresh)
+ token_types.map do |x|
+ Token.create!(subject: subject, audience: client, token_type: x)
end
end
end
app/models/user.rb
@@ -25,18 +25,11 @@ class User < ApplicationRecord
request.trusted? ? trusted_attributes_for(request) : {}
end
- def issue_tokens_to(client, token_type: :all)
+ def issue_tokens_to(client, token_types: [:access, :refresh])
transaction do
- if token_type == :all
- [
- Token.create!(subject: self, audience: client, token_type: :access),
- Token.create!(subject: self, audience: client, token_type: :refresh)
- ]
- elsif token_type == :access
- Token.create!(subject: self, audience: client, token_type: :access)
- elsif token_type == :refresh
- Token.create!(subject: self, audience: client, token_type: :refresh)
- end
+ token_types.map do |x|
+ Token.create!(subject: self, audience: client, token_type: x)
+ end
end
end