-
-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4976 from xihai01/api
Initial API Structure + Auth Route
- Loading branch information
Showing
28 changed files
with
378 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.