Skip to content

Commit c344ea4

Browse files
committed
Add endpoint for Profile API authorisation check
1 parent 793ba0b commit c344ea4

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module Api
4+
class ProfileAuthCheckController < ApiController
5+
def index
6+
authorised = ProfileApiClient.check_auth(token: current_user&.token)
7+
8+
render json: { can_use_profile_api: authorised }, status: :ok
9+
rescue ProfileApiClient::UnauthorizedError
10+
render json: { can_use_profile_api: false }, status: :ok
11+
end
12+
end
13+
end

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
post '/google/auth/exchange-code', to: 'google_auth#exchange_code', defaults: { format: :json }
9393

9494
resources :features, only: %i[index]
95+
96+
resources :profile_auth_check, only: %i[index]
9597
end
9698

9799
resource :github_webhooks, only: :create, defaults: { formats: :json }

lib/profile_api_client.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,21 @@ def initialize(response)
3636
@response_status = response.status
3737
@response_headers = response.headers
3838
@response_body = response.body
39-
4039
super("Unexpected response from Profile API (status code #{response.status})")
4140
end
4241
end
4342

4443
class << self
44+
def check_auth(token:)
45+
return true if ENV['BYPASS_OAUTH'].present?
46+
47+
response = connection(token).get('/api/v1/access')
48+
49+
response.status == 200
50+
rescue Faraday::BadRequestError, Faraday::UnauthorizedError
51+
false
52+
end
53+
4554
def create_school(token:, id:, code:)
4655
return { 'id' => id, 'schoolCode' => code } if ENV['BYPASS_OAUTH'].present?
4756

0 commit comments

Comments
 (0)