-
Notifications
You must be signed in to change notification settings - Fork 155
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 #171 from blackcandy-org/api
Add API for user authentication
- Loading branch information
Showing
20 changed files
with
254 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class ApiController < ApplicationController | ||
skip_before_action :verify_authenticity_token | ||
|
||
private | ||
|
||
# If user already has logged in then authenticate with current session, | ||
# otherwise authenticate with api token. | ||
def find_current_user | ||
Current.user = UserSession.find&.user | ||
|
||
return if logged_in? | ||
|
||
authenticate_with_http_token do |token, options| | ||
user = User.find_by(api_token: token) | ||
return unless user.present? | ||
|
||
# Compare the tokens in a time-constant manner, to mitigate timing attacks. | ||
Current.user = user if ActiveSupport::SecurityUtils.secure_compare(user.api_token, token) | ||
end | ||
end | ||
|
||
def require_login | ||
head :unauthorized unless logged_in? | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class AuthenticationsController < ApiController | ||
skip_before_action :find_current_user | ||
skip_before_action :require_login | ||
|
||
def create | ||
session = UserSession.new(session_params.merge({remember_me: true}).to_h) | ||
|
||
if params[:with_session] | ||
head :unauthorized unless session.save | ||
else | ||
head :unauthorized unless session.valid? | ||
end | ||
|
||
@current_user = User.find_by(email: session_params[:email]) | ||
head :unauthorized unless @current_user.present? | ||
|
||
@current_user.regenerate_api_token if @current_user.api_token.blank? | ||
end | ||
|
||
private | ||
|
||
def session_params | ||
params.require(:user_session).permit(:email, :password) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
class SongsController < ApiController | ||
def show | ||
@song = Song.find(params[:id]) | ||
@song_format = need_transcode?(@song.format) ? Stream::TRANSCODE_FORMAT : @song.format | ||
end | ||
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
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 @@ | ||
json.call(@current_user, :api_token) |
2 changes: 0 additions & 2 deletions
2
app/views/songs/show.json.jbuilder → app/views/api/v1/songs/show.json.jbuilder
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,6 @@ | ||
class AddApiTokenToUsers < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :users, :api_token, :string | ||
add_index :users, :api_token, unique: 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
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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class Api::V1::ApiControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@user = users(:visitor1) | ||
@song = songs(:mp3_sample) | ||
end | ||
|
||
test "should authenticate when have user session" do | ||
login(@user) | ||
get api_v1_song_url(@song), as: :json | ||
|
||
assert_response :success | ||
end | ||
|
||
test "should authenticate when have api token" do | ||
get api_v1_song_url(@song), as: :json, headers: api_token_header(@user) | ||
|
||
assert_response :success | ||
end | ||
|
||
test "should not authenticate when do not have user seesion or api token" do | ||
get api_v1_song_url(@song), as: :json | ||
|
||
assert_response :unauthorized | ||
end | ||
end |
67 changes: 67 additions & 0 deletions
67
test/controllers/api/v1/authentications_controller_test.rb
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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class Api::V1::AuthenticationsControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@user = users(:visitor1) | ||
end | ||
|
||
test "should create authentication without session" do | ||
post api_v1_authentication_url, as: :json, params: { | ||
user_session: { | ||
email: @user.email, | ||
password: "foobar" | ||
} | ||
} | ||
|
||
response = @response.parsed_body | ||
|
||
assert_response :success | ||
assert_nil session[:user_credentials] | ||
assert_equal @user.reload.api_token, response["api_token"] | ||
end | ||
|
||
test "should create authentication with session" do | ||
post api_v1_authentication_url, as: :json, params: { | ||
with_session: true, | ||
user_session: { | ||
email: @user.email, | ||
password: "foobar" | ||
} | ||
} | ||
|
||
response = @response.parsed_body | ||
|
||
assert_response :success | ||
assert_not_nil session[:user_credentials] | ||
assert_equal @user.reload.api_token, response["api_token"] | ||
end | ||
|
||
test "should not create authentication with wrong credential" do | ||
post api_v1_authentication_url, as: :json, params: { | ||
user_session: { | ||
email: @user.email, | ||
password: "fake" | ||
} | ||
} | ||
|
||
assert_response :unauthorized | ||
assert_nil session[:user_credentials] | ||
assert_empty @response.body | ||
end | ||
|
||
test "should not create authentication and session with wrong credential" do | ||
post api_v1_authentication_url, as: :json, params: { | ||
with_session: true, | ||
user_session: { | ||
email: @user.email, | ||
password: "fake" | ||
} | ||
} | ||
|
||
assert_response :unauthorized | ||
assert_nil session[:user_credentials] | ||
assert_empty @response.body | ||
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class Api::V1::SongsControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@song = songs(:mp3_sample) | ||
end | ||
|
||
test "should show song" do | ||
get api_v1_song_url(@song), as: :json, headers: api_token_header(users(:visitor1)) | ||
response = @response.parsed_body | ||
|
||
assert_response :success | ||
assert_equal @song.name, response["name"] | ||
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
Oops, something went wrong.