-
Notifications
You must be signed in to change notification settings - Fork 0
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 #45 from internetee/1-authenticating-users
implemented authorization
- Loading branch information
Showing
39 changed files
with
290 additions
and
719 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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
require_relative '../services/api_connector.rb' | ||
|
||
|
||
class HomeController < ApplicationController | ||
skip_before_action :authenticate_user!, :only => [:index] | ||
|
||
def index | ||
@quizzes = Quiz.all | ||
|
||
# test_request = ApiConnector.new(username: "oleghasjanov", password: "123456") | ||
# @result = test_request.get_pull_message(method: :get) | ||
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 |
---|---|---|
@@ -1,27 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
class Users::SessionsController < Devise::SessionsController | ||
# before_action :configure_sign_in_params, only: [:create] | ||
before_action :configure_sign_in_params, only: [:create] | ||
before_action :login_request, only: [:create] | ||
|
||
# GET /resource/sign_in | ||
# def new | ||
# super | ||
# end | ||
def new | ||
super | ||
end | ||
|
||
# POST /resource/sign_in | ||
# def create | ||
# super | ||
# end | ||
def create | ||
super | ||
end | ||
|
||
# DELETE /resource/sign_out | ||
# def destroy | ||
# super | ||
# end | ||
def destroy | ||
super | ||
end | ||
|
||
# protected | ||
protected | ||
|
||
# If you have extra params to permit, append them to the sanitizer. | ||
# def configure_sign_in_params | ||
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) | ||
# end | ||
def configure_sign_in_params | ||
devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) | ||
end | ||
|
||
def login_request | ||
user_request = ApiConnector.new(username: params[:user][:username], password: params[:user][:password]) | ||
result = user_request.sign_in | ||
|
||
checking_username(result) | ||
end | ||
|
||
def checking_username(result) | ||
if result["code"] == 1000 | ||
username = result["data"]["username"] | ||
user = User.find_by(username: username) | ||
|
||
if user.present? | ||
sign_in user | ||
else | ||
new_user = User.create!( | ||
username: username, | ||
superadmin_role: false) | ||
|
||
sign_in new_user | ||
Rails.logger.info "#{user.username} sign in" | ||
end | ||
|
||
else | ||
Rails.logger.info "Fails to sign in" | ||
# p "Fails to sign in" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'faraday' | ||
|
||
class ApiConnector | ||
attr_reader :auth_token | ||
|
||
POLL_MESSAGE_ENDPOINT = "http://registry:3000/repp/v1/registrar/accreditation_info" | ||
|
||
def initialize(username:, password:) | ||
@auth_token = generate_token(username: username, password: password) | ||
end | ||
|
||
def sign_in(params: nil) | ||
request = faraday_request(url: POLL_MESSAGE_ENDPOINT, params: params) | ||
response = request.send(:get) | ||
JSON.parse(response.body) | ||
end | ||
|
||
private | ||
|
||
def generate_token(username:, password:) | ||
Base64.urlsafe_encode64("#{username}:#{password}") | ||
end | ||
|
||
def faraday_request(url:, params: {}) | ||
Faraday.new( | ||
url: url, | ||
headers: { 'Authorization' => "Basic #{@auth_token}" }, | ||
params: params, | ||
ssl: { verify: false} | ||
) | ||
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.