Skip to content

Commit 353de45

Browse files
committed
Add assignment backend
1 parent 7dfb273 commit 353de45

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1197
-0
lines changed

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM ruby:2.3.3
2+
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
3+
RUN mkdir /myapp
4+
WORKDIR /myapp
5+
COPY Gemfile /myapp/Gemfile
6+
COPY Gemfile.lock /myapp/Gemfile.lock
7+
RUN bundle install
8+
COPY . /myapp

Gemfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
source 'https://rubygems.org'
2+
3+
4+
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
5+
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
6+
# Use postgresql as the database for Active Record
7+
gem 'pg', '~> 0.18'
8+
# Use Puma as the app server
9+
gem 'puma', '~> 3.0'
10+
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
11+
# gem 'jbuilder', '~> 2.5'
12+
# Use Redis adapter to run Action Cable in production
13+
# gem 'redis', '~> 3.0'
14+
# Use ActiveModel has_secure_password
15+
gem 'bcrypt', '~> 3.1.7'
16+
gem 'jwt'
17+
18+
# Use Capistrano for deployment
19+
# gem 'capistrano-rails', group: :development
20+
21+
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
22+
gem 'rack-cors'
23+
24+
group :development, :test do
25+
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
26+
gem 'byebug', platform: :mri
27+
end
28+
29+
group :development do
30+
gem 'listen', '~> 3.0.5'
31+
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
32+
gem 'spring'
33+
gem 'spring-watcher-listen', '~> 2.0.0'
34+
end
35+
36+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
37+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Gemfile.lock

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
actioncable (5.0.0.1)
5+
actionpack (= 5.0.0.1)
6+
nio4r (~> 1.2)
7+
websocket-driver (~> 0.6.1)
8+
actionmailer (5.0.0.1)
9+
actionpack (= 5.0.0.1)
10+
actionview (= 5.0.0.1)
11+
activejob (= 5.0.0.1)
12+
mail (~> 2.5, >= 2.5.4)
13+
rails-dom-testing (~> 2.0)
14+
actionpack (5.0.0.1)
15+
actionview (= 5.0.0.1)
16+
activesupport (= 5.0.0.1)
17+
rack (~> 2.0)
18+
rack-test (~> 0.6.3)
19+
rails-dom-testing (~> 2.0)
20+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
21+
actionview (5.0.0.1)
22+
activesupport (= 5.0.0.1)
23+
builder (~> 3.1)
24+
erubis (~> 2.7.0)
25+
rails-dom-testing (~> 2.0)
26+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
27+
activejob (5.0.0.1)
28+
activesupport (= 5.0.0.1)
29+
globalid (>= 0.3.6)
30+
activemodel (5.0.0.1)
31+
activesupport (= 5.0.0.1)
32+
activerecord (5.0.0.1)
33+
activemodel (= 5.0.0.1)
34+
activesupport (= 5.0.0.1)
35+
arel (~> 7.0)
36+
activesupport (5.0.0.1)
37+
concurrent-ruby (~> 1.0, >= 1.0.2)
38+
i18n (~> 0.7)
39+
minitest (~> 5.1)
40+
tzinfo (~> 1.1)
41+
arel (7.1.4)
42+
bcrypt (3.1.11)
43+
builder (3.2.3)
44+
byebug (9.1.0)
45+
concurrent-ruby (1.0.5)
46+
crass (1.0.3)
47+
erubis (2.7.0)
48+
ffi (1.9.18)
49+
globalid (0.4.1)
50+
activesupport (>= 4.2.0)
51+
i18n (0.9.1)
52+
concurrent-ruby (~> 1.0)
53+
jwt (2.1.0)
54+
listen (3.0.8)
55+
rb-fsevent (~> 0.9, >= 0.9.4)
56+
rb-inotify (~> 0.9, >= 0.9.7)
57+
loofah (2.1.1)
58+
crass (~> 1.0.2)
59+
nokogiri (>= 1.5.9)
60+
mail (2.7.0)
61+
mini_mime (>= 0.1.1)
62+
method_source (0.9.0)
63+
mini_mime (1.0.0)
64+
mini_portile2 (2.3.0)
65+
minitest (5.10.3)
66+
nio4r (1.2.1)
67+
nokogiri (1.8.1)
68+
mini_portile2 (~> 2.3.0)
69+
pg (0.21.0)
70+
puma (3.11.0)
71+
rack (2.0.3)
72+
rack-cors (1.0.2)
73+
rack-test (0.6.3)
74+
rack (>= 1.0)
75+
rails (5.0.0.1)
76+
actioncable (= 5.0.0.1)
77+
actionmailer (= 5.0.0.1)
78+
actionpack (= 5.0.0.1)
79+
actionview (= 5.0.0.1)
80+
activejob (= 5.0.0.1)
81+
activemodel (= 5.0.0.1)
82+
activerecord (= 5.0.0.1)
83+
activesupport (= 5.0.0.1)
84+
bundler (>= 1.3.0, < 2.0)
85+
railties (= 5.0.0.1)
86+
sprockets-rails (>= 2.0.0)
87+
rails-dom-testing (2.0.3)
88+
activesupport (>= 4.2.0)
89+
nokogiri (>= 1.6)
90+
rails-html-sanitizer (1.0.3)
91+
loofah (~> 2.0)
92+
railties (5.0.0.1)
93+
actionpack (= 5.0.0.1)
94+
activesupport (= 5.0.0.1)
95+
method_source
96+
rake (>= 0.8.7)
97+
thor (>= 0.18.1, < 2.0)
98+
rake (12.3.0)
99+
rb-fsevent (0.10.2)
100+
rb-inotify (0.9.10)
101+
ffi (>= 0.5.0, < 2)
102+
spring (2.0.2)
103+
activesupport (>= 4.2)
104+
spring-watcher-listen (2.0.1)
105+
listen (>= 2.7, < 4.0)
106+
spring (>= 1.2, < 3.0)
107+
sprockets (3.7.1)
108+
concurrent-ruby (~> 1.0)
109+
rack (> 1, < 3)
110+
sprockets-rails (3.2.1)
111+
actionpack (>= 4.0)
112+
activesupport (>= 4.0)
113+
sprockets (>= 3.0.0)
114+
thor (0.20.0)
115+
thread_safe (0.3.6)
116+
tzinfo (1.2.4)
117+
thread_safe (~> 0.1)
118+
websocket-driver (0.6.5)
119+
websocket-extensions (>= 0.1.0)
120+
websocket-extensions (0.1.3)
121+
122+
PLATFORMS
123+
ruby
124+
125+
DEPENDENCIES
126+
bcrypt (~> 3.1.7)
127+
byebug
128+
jwt
129+
listen (~> 3.0.5)
130+
pg (~> 0.18)
131+
puma (~> 3.0)
132+
rack-cors
133+
rails (~> 5.0.0, >= 5.0.0.1)
134+
spring
135+
spring-watcher-listen (~> 2.0.0)
136+
tzinfo-data
137+
138+
BUNDLED WITH
139+
1.14.6

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require_relative 'config/application'
5+
6+
Rails.application.load_tasks

app/auth/authenticate_user.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class AuthenticateUser
2+
def initialize(email, password)
3+
@email = email
4+
@password = password
5+
end
6+
7+
def call
8+
JsonWebToken.encode(user_id: user.id) if user
9+
end
10+
11+
private
12+
13+
attr_reader :email, :password
14+
15+
def user
16+
user = User.find_by(email: email)
17+
return user if user && user.authenticate(password)
18+
raise(ExceptionHandler::InvalidToken, 'Invalid credentials')
19+
end
20+
end

app/auth/authorize_api_request.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class AuthorizeApiRequest
2+
def initialize(headers = {})
3+
@headers = headers
4+
end
5+
6+
def call
7+
{
8+
user: user
9+
}
10+
end
11+
12+
private
13+
14+
attr_reader :headers
15+
16+
def user
17+
@user ||= User.find(decoded_auth_token[:user_id]) if decoded_auth_token
18+
end
19+
20+
def decoded_auth_token
21+
@decoded_auth_token ||= JsonWebToken.decode http_auth_header
22+
end
23+
24+
def http_auth_header
25+
raise(ExceptionHandler::MissingToken, 'Missing Token') unless headers['Authorization'].present?
26+
headers['Authorization'].split(' ').last
27+
end
28+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ApplicationCable
2+
class Channel < ActionCable::Channel::Base
3+
end
4+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module ApplicationCable
2+
class Connection < ActionCable::Connection::Base
3+
end
4+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class ApplicationController < ActionController::API
2+
include ExceptionHandler
3+
4+
before_action :authorize_request
5+
attr_reader :current_user
6+
7+
private
8+
9+
def authorize_request
10+
@current_user = (AuthorizeApiRequest.new(request.headers).call)[:user]
11+
end
12+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class AuthenticationController < ApplicationController
2+
skip_before_action :authorize_request, only: :authenticate
3+
4+
def authenticate
5+
auth_token = AuthenticateUser.new(authentication_params[:email], authentication_params[:password]).call
6+
render json: {auth_token: auth_token}, status: :ok
7+
end
8+
9+
private
10+
11+
def authentication_params
12+
params.permit(:email, :password)
13+
end
14+
end

0 commit comments

Comments
 (0)