-
Notifications
You must be signed in to change notification settings - Fork 2
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 #116 from zhishi-project/ft-user-authentication-13…
…7599493 #137599493 user authentication
- Loading branch information
Showing
30 changed files
with
218 additions
and
254 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,30 @@ | ||
|
||
GOOGLE_CLIENT_ID: '<client_id>' | ||
GOOGLE_CLIENT_SECRET: '<client_secret>' | ||
BUGSNAG_API_KEY: <api_key> | ||
BASE_ANDELA_URL: '<url>' | ||
|
||
development: | ||
SLACK_CLIENT_ID: '<client_id>' | ||
SLACK_CLIENT_SECRET: <client_secret> | ||
ELASTIC_SEARCH_HOST_URL: <host_url> | ||
REDISTOGO_URL: <url> | ||
ZI_NOTIFICATION_URL: <notification_url> | ||
BASE_URL: <base_url> | ||
ANDELA_AUTH_URL: <auth_url> | ||
NEW_RELIC_LICENSE_KEY: <license_key> | ||
|
||
staging: | ||
ZI_NOTIFICATION_URL: <notification_url> | ||
BASE_URL: <base_url> | ||
ANDELA_AUTH_URL: <auth_url> | ||
|
||
production: | ||
ZI_NOTIFICATION_URL: <notification_url> | ||
BASE_URL: <base_url> | ||
ANDELA_AUTH_URL: <auth_url> | ||
|
||
test: | ||
ZI_NOTIFICATION_URL: <notification_url> | ||
BASE_URL: <base_url> | ||
ANDELA_AUTH_URL: <auth_url> |
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,32 @@ | ||
class AndelaAuthV2 | ||
attr_reader :response | ||
|
||
def self.authenticate(token) | ||
conn = Connection::FaradayConnection.connection(token) | ||
response = conn.get('/api/v1/users/me') | ||
new(response) | ||
end | ||
|
||
def initialize(response) | ||
@response = response | ||
end | ||
|
||
def body | ||
@body ||= JSON.parse(response.body) | ||
end | ||
|
||
def authenticated? | ||
# check response status and body | ||
!body.include? 'error' | ||
end | ||
|
||
def current_user | ||
if authenticated? | ||
body | ||
else | ||
raise UserNotFoundError.new('User could not be found') | ||
end | ||
end | ||
|
||
class UserNotFoundError < StandardError; 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,24 @@ | ||
require_relative 'andela_auth_v2' | ||
|
||
class Authenticator | ||
attr_reader :request | ||
attr_accessor :user | ||
|
||
def initialize(request) | ||
@request = request | ||
end | ||
|
||
def authenticated? | ||
if request.headers['Authorization'] | ||
strategy, token = request.headers['Authorization'].split | ||
auth = AndelaAuthV2.authenticate(token) | ||
if strategy == 'Bearer' && auth.authenticated? | ||
return @user = auth.current_user | ||
else | ||
return false | ||
end | ||
else | ||
return false | ||
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,23 @@ | ||
class Connection::FaradayConnection | ||
BASE_ANDELA_URL = ENV['BASE_ANDELA_URL'] # use env variable to set the url | ||
|
||
def self.connection(token) | ||
options = { | ||
headers: { | ||
"Authorization" => "Bearer #{token}", | ||
"Accept" => 'application/json; charset=utf-8', | ||
}, | ||
ssl: { | ||
verify: false | ||
} | ||
} | ||
|
||
::Faraday::Connection.new(BASE_ANDELA_URL, options) do |conn| | ||
conn.use ::Faraday::Request::Multipart | ||
conn.use ::Faraday::Request::UrlEncoded | ||
conn.use FaradayMiddleware::FollowRedirects | ||
conn.response :logger, Rails.logger | ||
conn.adapter ::Faraday.default_adapter | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
json.partial! "users/user", user: @current_user | ||
json.partial! 'tags/tag', tags: @current_user.tags | ||
json.api_key @token | ||
|
||
# json.user { json.partial! 'users/user', user: @current_user } | ||
# json.api_key @token |
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
Oops, something went wrong.