mirror of https://github.com/mastodon/mastodon
Add userinfo oauth endpoint (#32548)
parent
0a599d08d8
commit
e1b7382ea6
@ -0,0 +1,11 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Oauth::UserinfoController < Api::BaseController
|
||||||
|
before_action -> { doorkeeper_authorize! :profile }, only: [:show]
|
||||||
|
before_action :require_user!
|
||||||
|
|
||||||
|
def show
|
||||||
|
@account = current_account
|
||||||
|
render json: @account, serializer: OauthUserinfoSerializer
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,31 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class OauthUserinfoSerializer < ActiveModel::Serializer
|
||||||
|
include RoutingHelper
|
||||||
|
|
||||||
|
attributes :iss, :sub, :name, :preferred_username, :profile, :picture
|
||||||
|
|
||||||
|
def iss
|
||||||
|
root_url
|
||||||
|
end
|
||||||
|
|
||||||
|
def sub
|
||||||
|
ActivityPub::TagManager.instance.uri_for(object)
|
||||||
|
end
|
||||||
|
|
||||||
|
def name
|
||||||
|
object.display_name
|
||||||
|
end
|
||||||
|
|
||||||
|
def preferred_username
|
||||||
|
object.username
|
||||||
|
end
|
||||||
|
|
||||||
|
def profile
|
||||||
|
ActivityPub::TagManager.instance.url_for(object)
|
||||||
|
end
|
||||||
|
|
||||||
|
def picture
|
||||||
|
full_asset_url(object.avatar_original_url)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,51 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe 'Oauth Userinfo Endpoint' do
|
||||||
|
include RoutingHelper
|
||||||
|
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
let(:account) { user.account }
|
||||||
|
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
|
||||||
|
let(:scopes) { 'profile' }
|
||||||
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
||||||
|
|
||||||
|
shared_examples 'returns successfully' do
|
||||||
|
it 'returns http success' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(response.content_type).to start_with('application/json')
|
||||||
|
expect(response.parsed_body).to include({
|
||||||
|
iss: root_url,
|
||||||
|
sub: account_url(account),
|
||||||
|
name: account.display_name,
|
||||||
|
preferred_username: account.username,
|
||||||
|
profile: short_account_url(account),
|
||||||
|
picture: full_asset_url(account.avatar_original_url),
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET /oauth/userinfo' do
|
||||||
|
subject do
|
||||||
|
get '/oauth/userinfo', headers: headers
|
||||||
|
end
|
||||||
|
|
||||||
|
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
|
||||||
|
it_behaves_like 'returns successfully'
|
||||||
|
end
|
||||||
|
|
||||||
|
# As this is borrowed from OpenID, the specification says we must also support
|
||||||
|
# POST for the userinfo endpoint:
|
||||||
|
# https://openid.net/specs/openid-connect-core-1_0.html#UserInfo
|
||||||
|
describe 'POST /oauth/userinfo' do
|
||||||
|
subject do
|
||||||
|
post '/oauth/userinfo', headers: headers
|
||||||
|
end
|
||||||
|
|
||||||
|
it_behaves_like 'forbidden for wrong scope', 'read:accounts'
|
||||||
|
it_behaves_like 'returns successfully'
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue