-
-
Notifications
You must be signed in to change notification settings - Fork 529
Initial API Structure + Auth Route #4976
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 all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8ecf724
implement api structure; add auth routes
xihai01 fd4603e
lint files
xihai01 602e4cf
fix some checks like codeclimate and adding extra tests
xihai01 afd80e6
Merge branch 'main' into api
xihai01 12be2da
change file paths of tests so spec checker is satisfied
xihai01 3ca2d30
Merge branch 'api' of github.com:xihai01/casa into api
xihai01 de18953
add rswag gem for api documentation
xihai01 83bb78f
update gemfile; add blueprinter and remove active model serializer
xihai01 1c5bb65
remove active model serializer files
xihai01 3fa1bd1
update files with blueprinter and create request specs
xihai01 9fadddd
remove comments and add lint files
xihai01 675c22c
Merge branch 'main' into api
xihai01 8bb45ab
resolve pr changes
xihai01 048c8e2
Merge branch 'main' into api
xihai01 121c689
Merge branch 'main' into api
xihai01 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
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,5 @@ | ||
| class Api::V1::SessionBlueprint < Blueprinter::Base | ||
| identifier :id | ||
|
|
||
| fields :id, :display_name, :email, :token | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Api::V1::BaseController < ActionController::API | ||
| rescue_from ActiveRecord::RecordNotFound, with: :not_found | ||
| before_action :authenticate_user!, except: [:create] | ||
|
|
||
| def authenticate_user! | ||
| token, options = ActionController::HttpAuthentication::Token.token_and_options(request) | ||
| user = User.find_by(email: options[:email]) | ||
| if user && token && ActiveSupport::SecurityUtils.secure_compare(user.token, token) | ||
| @current_user = user | ||
| else | ||
| render json: {message: "Wrong password or email"}, status: 401 | ||
| end | ||
| end | ||
|
|
||
| def not_found | ||
| api_error(status: 404, errors: "Not found") | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| class Api::V1::Users::SessionsController < Api::V1::BaseController | ||
| def create | ||
| load_resource | ||
| if @user | ||
| render json: Api::V1::SessionBlueprint.render(@user), status: 201 | ||
| else | ||
| render json: {message: "Wrong password or email"}, status: 401 | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def user_params | ||
| params.permit(:email, :password) | ||
| end | ||
|
|
||
| def load_resource | ||
| @user = User.find_by(email: user_params[:email]) | ||
| unless @user&.valid_password?(user_params[:password]) | ||
| @user = nil | ||
| 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
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| svCtLWmi6TUWfy4jhsNxZgGKdzBrjq5JjKkGUaDA5tlP2XFn6XY8lJDVhF+T82kGjwT4EgsBheMZqPMbytlJ6iSDBIq/bHfjl1E5Zx3DqCkd4gDYgVK0roJffesKQPuWUSQUzvJV9pZ9VQEKbh+YA/I/N6aWGbkYlKXTOPHMY7F+rfiKXb8vHodUGWxCTycsWLpe/ohBvF7zzSwxkG7sEmbnRnqYd2Tmn0ASf6vNKXOzPamQ21rrgUss427/zjCjzWHCk4iUaHnhQQYwC2zJ+m1/0Uu+sM5CkYJhddsPbeeQkd7vgPjHBylgkT6L86XTz8sBrQDZB51TbmNouygu96NzQwE472c0csFEWwjz7fepy7sZkHN5KqQ=--dx6D/QqFOeacGYGg--+r3ffqcg8wONL9oMId9u5g== | ||
| aewvdbZoQz8v7s3UlJ/+XOIrxpj1/nP2/dA7FkLGvTgmu8lZrnyecC19sDE6bcZN4XsnIqDomjSg/CL8TefHKXOsaoNNKmW8YPVfoH8AmlqXxvJduiZNuXlOcf7SR01E7E0r1VIdRga6g9KtOHBbgtc6hQyOs/2ajSxbD3gY5IFWnWNHIqMEWMUMy/PXtSSxUr+FdNCgdod9Rx0EEiecfEz1tMBP/V69dRwSrM5yfTeogkUPpOqReFisTbn9f0yolmNhhxo7nPoPzyeEcGHl4+maS1GHa6uYQ2n2d2t34FmhcDttI+rV7ITU9LmuwVcjgCE9fPxMUZ9bX2UBUEHialBZ8S+izXyBAKGTvbQw+/Wk9KNT98Tl3Gg=--BRmMgMTOgyAZUyw4--2OyLty/a3xH0OjlI0sf9Yw== |
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,5 @@ | ||
| require "oj" # you can skip this if OJ has already been required. | ||
|
|
||
| Blueprinter.configure do |config| | ||
| config.generator = Oj # default is JSON | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Rails.application.config.middleware.insert_before 0, Rack::Cors do | ||
| allow do | ||
| origins "*" # make sure to change to domain name of frontend | ||
| resource "/api/v1/*", headers: :any, methods: [:get, :post, :patch, :put, :delete, :options, :head] | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| Rswag::Api.configure do |c| | ||
| # Specify a root folder where Swagger JSON files are located | ||
| # This is used by the Swagger middleware to serve requests for API descriptions | ||
| # NOTE: If you're using rswag-specs to generate Swagger, you'll need to ensure | ||
| # that it's configured to generate files in the same folder | ||
| c.swagger_root = Rails.root.to_s + "/swagger" | ||
|
|
||
| # Inject a lambda function to alter the returned Swagger prior to serialization | ||
| # The function will have access to the rack env for the current request | ||
| # For example, you could leverage this to dynamically assign the "host" property | ||
| # | ||
| # c.swagger_filter = lambda { |swagger, env| swagger['host'] = env['HTTP_HOST'] } | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| Rswag::Ui.configure do |c| | ||
| # List the Swagger endpoints that you want to be documented through the | ||
| # swagger-ui. The first parameter is the path (absolute or relative to the UI | ||
| # host) to the corresponding endpoint and the second is a title that will be | ||
| # displayed in the document selector. | ||
| # NOTE: If you're using rspec-api to expose Swagger files | ||
| # (under swagger_root) as JSON or YAML endpoints, then the list below should | ||
| # correspond to the relative paths for those endpoints. | ||
|
|
||
| c.swagger_endpoint "/api-docs/v1/swagger.yaml", "API V1 Docs" | ||
|
|
||
| # Add Basic Auth in case your API is private | ||
| # c.basic_auth_enabled = true | ||
| # c.basic_auth_credentials 'username', 'password' | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class AddTokenToUsers < ActiveRecord::Migration[7.0] | ||
| def up | ||
| add_column :users, :token, :string | ||
| end | ||
|
|
||
| def down | ||
| remove_column :users, :token, :string | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class DropJwtDenylistTable < ActiveRecord::Migration[7.0] | ||
| def change | ||
| drop_table :jwt_denylist, if_exists: true | ||
| 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
14 changes: 14 additions & 0 deletions
14
lib/tasks/deployment/20230822145532_populate_api_tokens.rake
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,14 @@ | ||
| namespace :after_party do | ||
| desc "Deployment task: populate_api_tokens" | ||
| task populate_api_tokens: :environment do | ||
| puts "Running deploy task 'populate_api_tokens'" unless Rails.env.test? | ||
|
|
||
| # Put your task implementation HERE. | ||
| User.find_each { |user| user.save! } | ||
|
|
||
| # Update task as completed. If you remove the line below, the task will | ||
| # run with every deploy (or every time you call after_party:run). | ||
| AfterParty::TaskRecord | ||
| .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe "Base Controller", type: :request do | ||
| before do | ||
| base_controller = Class.new(Api::V1::BaseController) do | ||
| def index | ||
| render json: {message: "Successfully autenticated"} | ||
| end | ||
| end | ||
| stub_const("BaseController", base_controller) | ||
| Rails.application.routes.disable_clear_and_finalize = true | ||
| Rails.application.routes.draw do | ||
| get "/index", to: "base#index" | ||
| end | ||
| end | ||
|
|
||
| after { Rails.application.reload_routes! } | ||
|
|
||
| # test authenticate_user! works | ||
| describe "GET #index" do | ||
| let(:user) { create(:volunteer) } | ||
| it "returns http success when valid credentials" do | ||
| get "/index", headers: {"Authorization" => "Token token=#{user.token}, email=#{user.email}"} | ||
| expect(response).to have_http_status(:success) | ||
| expect(response.body).to eq({message: "Successfully autenticated"}.to_json) | ||
| end | ||
| it "returns http unauthorized if invalid token" do | ||
| get "/index", headers: {"Authorization" => "Token token=, email=#{user.email}"} | ||
| expect(response).to have_http_status(:unauthorized) | ||
| expect(response.body).to eq({message: "Wrong password or email"}.to_json) | ||
| 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.
This is okay for now, but let's remove once we have a route we can actually test authentication against.
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.
alright.