-
Notifications
You must be signed in to change notification settings - Fork 5
1004: Add new endpoint for google token exchange #609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a3bed8f
1004: Add new endpoint for google token exchange
danhalson 91f13ce
Merge branch 'main' into danhalson/1004-update-google-auth
danhalson 3120f1c
1004: Add an explicit timeout to the call
danhalson c5f0faf
Add fallback in the unlikely scenario error_description doesn't exist
danhalson 96f6b57
1004: Raise a bad_gateway if the json response from google is badly f…
danhalson a8e4345
1004: Reduce controller actionc complexity by using smaller methods
danhalson 9d919d4
Merge branch 'main' into danhalson/1004-update-google-auth
danhalson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,45 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Api | ||
| class GoogleAuthController < ApiController | ||
| TOKEN_EXCHANGE_URL = 'https://oauth2.googleapis.com/token' | ||
|
|
||
| before_action :authorize_user | ||
| authorize_resource :google_auth, class: false | ||
|
|
||
| def exchange_code | ||
| payload = google_token_params | ||
|
|
||
| request_body = { | ||
| code: payload[:code], | ||
| client_id: ENV.fetch('GOOGLE_CLIENT_ID'), | ||
| client_secret: ENV.fetch('GOOGLE_CLIENT_SECRET'), | ||
| redirect_uri: payload[:redirect_uri], | ||
| grant_type: 'authorization_code' | ||
| } | ||
|
|
||
| conn = Faraday.new do |f| | ||
| f.request :url_encoded | ||
danhalson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| response = conn.post(TOKEN_EXCHANGE_URL, request_body) | ||
| @token_response = JSON.parse(response.body) | ||
danhalson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if response.success? | ||
| render :exchange_code, status: :ok | ||
| else | ||
| render json: { error: @token_response['error_description'] }, status: :unauthorized | ||
danhalson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
| rescue Faraday::Error => e | ||
| render json: { error: e.message }, status: :service_unavailable | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def google_token_params | ||
| params.require(:google_auth).require(:code) | ||
| params.require(:google_auth).require(:redirect_uri) | ||
| params.require(:google_auth).permit(:code, :redirect_uri) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| json.call( | ||
| @token_response, | ||
| 'access_token', | ||
| 'expires_in', | ||
| 'token_type', | ||
| 'scope', | ||
| 'id_token' | ||
danhalson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
This file contains hidden or 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 hidden or 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 hidden or 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,184 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Google Auth requests' do | ||
| let(:headers) { { Authorization: UserProfileMock::TOKEN } } | ||
| let(:school) { create(:school) } | ||
| let(:owner) { create(:owner, school:) } | ||
|
|
||
| before do | ||
| authenticated_in_hydra_as(owner) | ||
| end | ||
|
|
||
| describe 'POST /api/google_auth/exchange_code' do | ||
| let(:params) do | ||
| { | ||
| google_auth: { | ||
| code: 'test-authorization-code', | ||
| redirect_uri: 'https://example.com/callback' | ||
| } | ||
| } | ||
| end | ||
|
|
||
| let(:google_token_response) do | ||
| { | ||
| 'access_token' => 'test-access-token', | ||
| 'expires_in' => 3599, | ||
| 'token_type' => 'Bearer', | ||
| 'scope' => 'openid email profile', | ||
| 'id_token' => 'test-id-token' | ||
| } | ||
| end | ||
|
|
||
| around do |example| | ||
| ClimateControl.modify( | ||
| GOOGLE_CLIENT_ID: 'test-client-id', | ||
| GOOGLE_CLIENT_SECRET: 'test-client-secret' | ||
| ) do | ||
| example.run | ||
| end | ||
| end | ||
|
|
||
| context 'when token exchange is successful' do | ||
| before do | ||
| stub_request(:post, Api::GoogleAuthController::TOKEN_EXCHANGE_URL) | ||
| .with( | ||
| body: { | ||
| code: 'test-authorization-code', | ||
| client_id: 'test-client-id', | ||
| client_secret: 'test-client-secret', | ||
| redirect_uri: 'https://example.com/callback', | ||
| grant_type: 'authorization_code' | ||
| } | ||
| ) | ||
| .to_return( | ||
| status: 200, | ||
| body: google_token_response.to_json, | ||
| headers: { 'Content-Type' => 'application/json' } | ||
| ) | ||
| end | ||
|
|
||
| it 'returns success response' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response).to have_http_status(:ok) | ||
| end | ||
|
|
||
| it 'returns token response from Google' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response.parsed_body).to eq(google_token_response) | ||
| end | ||
|
|
||
| it 'includes access_token in response' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response.parsed_body['access_token']).to eq('test-access-token') | ||
| end | ||
| end | ||
|
|
||
| context 'when token exchange fails with error from Google' do | ||
| let(:error_response) do | ||
| { | ||
| 'error' => 'invalid_grant', | ||
| 'error_description' => 'Bad Request' | ||
| } | ||
| end | ||
|
|
||
| before do | ||
| stub_request(:post, Api::GoogleAuthController::TOKEN_EXCHANGE_URL) | ||
| .to_return( | ||
| status: 400, | ||
| body: error_response.to_json, | ||
| headers: { 'Content-Type' => 'application/json' } | ||
| ) | ||
| end | ||
|
|
||
| it 'returns unauthorized response' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response).to have_http_status(:unauthorized) | ||
| end | ||
|
|
||
| it 'returns error message' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response.parsed_body['error']).to eq('Bad Request') | ||
| end | ||
| end | ||
|
|
||
| context 'when network error occurs' do | ||
| before do | ||
| stub_request(:post, Api::GoogleAuthController::TOKEN_EXCHANGE_URL) | ||
| .to_raise(Faraday::ConnectionFailed.new('Connection failed')) | ||
| end | ||
|
|
||
| it 'returns service unavailable response' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response).to have_http_status(:service_unavailable) | ||
| end | ||
|
|
||
| it 'returns error message' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response.parsed_body['error']).to eq('Connection failed') | ||
| end | ||
| end | ||
|
|
||
| context 'when code parameter is missing' do | ||
| let(:params) do | ||
| { | ||
| google_auth: { | ||
| redirect_uri: 'https://example.com/callback' | ||
| } | ||
| } | ||
| end | ||
|
|
||
| it 'returns bad request response' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response).to have_http_status(:bad_request) | ||
| end | ||
| end | ||
|
|
||
| context 'when redirect_uri parameter is missing' do | ||
| let(:params) do | ||
| { | ||
| google_auth: { | ||
| code: 'test-authorization-code' | ||
| } | ||
| } | ||
| end | ||
|
|
||
| it 'returns bad request response' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response).to have_http_status(:bad_request) | ||
| end | ||
| end | ||
|
|
||
| context 'when google_auth params are missing' do | ||
| it 'returns bad request response' do | ||
| post('/api/google/auth/exchange-code', headers:) | ||
| expect(response).to have_http_status(:bad_request) | ||
| end | ||
| end | ||
|
|
||
| context 'when user is not authenticated' do | ||
| before do | ||
| unauthenticated_in_hydra | ||
| end | ||
|
|
||
| it 'returns unauthorized response' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response).to have_http_status(:unauthorized) | ||
| end | ||
| end | ||
|
|
||
| context 'when user is not authorized' do | ||
| let(:student) { create(:student, school:) } | ||
|
|
||
| before do | ||
| authenticated_in_hydra_as(student) | ||
| end | ||
|
|
||
| it 'returns forbidden response' do | ||
| post('/api/google/auth/exchange-code', params:, headers:) | ||
| expect(response).to have_http_status(:forbidden) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My only thought is that this method is quite long and whether all this needs to be in the controller, or if some of this (like sending the actual request) could be factored out into another file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather avoid the boiler plate and added complexity of factoring it into a poro via a service or something, but have split the code into smaller methods, is that ok?