Description:
authenticates through programming.in.th
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r770:572d003cd44c - - 3 files changed: 59 inserted, 1 deleted
@@ -0,0 +1,33 | |||||
|
|
1 | + # Authentication and user imports through programming.in.th web request | ||
|
|
2 | + require 'net/http' | ||
|
|
3 | + require 'uri' | ||
|
|
4 | + require 'json' | ||
|
|
5 | + | ||
|
|
6 | + class ProgrammingAuthenticator | ||
|
|
7 | + PROGRAMMING_AUTHEN_URL = "https://programming.in.th/authen.php" | ||
|
|
8 | + | ||
|
|
9 | + def find_or_create_user(result) | ||
|
|
10 | + user = User.find_by(login: result['username']) | ||
|
|
11 | + if not user | ||
|
|
12 | + user = User.new(login: result['username'], | ||
|
|
13 | + full_name: result['firstname'] + ' ' + result['surname'], | ||
|
|
14 | + alias: result['display'], | ||
|
|
15 | + email: result['email']) | ||
|
|
16 | + user.password = User.random_password | ||
|
|
17 | + user.save | ||
|
|
18 | + end | ||
|
|
19 | + return user | ||
|
|
20 | + end | ||
|
|
21 | + | ||
|
|
22 | + def authenticate(login, password) | ||
|
|
23 | + uri = URI(PROGRAMMING_AUTHEN_URL) | ||
|
|
24 | + result = Net::HTTP.post_form(uri, 'username' => login, 'password' => password) | ||
|
|
25 | + request_result = JSON.parse(result.body) | ||
|
|
26 | + | ||
|
|
27 | + if request_result.fetch('status', 'incorrect') == 'OK' | ||
|
|
28 | + return find_or_create_user(request_result) | ||
|
|
29 | + else | ||
|
|
30 | + return nil | ||
|
|
31 | + end | ||
|
|
32 | + end | ||
|
|
33 | + end |
@@ -1,5 +1,7 | |||||
|
1 | class LoginController < ApplicationController |
|
1 | class LoginController < ApplicationController |
|
2 |
|
2 | ||
|
|
3 | + @@authenticators = [] | ||
|
|
4 | + | ||
|
3 | def index |
|
5 | def index |
|
4 | # show login screen |
|
6 | # show login screen |
|
5 | reset_session |
|
7 | reset_session |
@@ -7,7 +9,7 | |||||
|
7 | end |
|
9 | end |
|
8 |
|
10 | ||
|
9 | def login |
|
11 | def login |
|
10 |
- user = |
|
12 | + user = get_authenticated_user(params[:login], params[:password]) |
|
11 | unless user |
|
13 | unless user |
|
12 | flash[:notice] = 'Wrong password' |
|
14 | flash[:notice] = 'Wrong password' |
|
13 | redirect_to :controller => 'main', :action => 'login' |
|
15 | redirect_to :controller => 'main', :action => 'login' |
@@ -60,4 +62,24 | |||||
|
60 | end |
|
62 | end |
|
61 | end |
|
63 | end |
|
62 |
|
64 | ||
|
|
65 | + def self.add_authenticator(authenticator) | ||
|
|
66 | + @@authenticators << authenticator | ||
|
|
67 | + end | ||
|
|
68 | + | ||
|
|
69 | + protected | ||
|
|
70 | + | ||
|
|
71 | + def get_authenticated_user(login, password) | ||
|
|
72 | + if @@authenticators.empty? | ||
|
|
73 | + return User.authenticate(login, password) | ||
|
|
74 | + else | ||
|
|
75 | + user = nil | ||
|
|
76 | + @@authenticators.each do |authenticator| | ||
|
|
77 | + if not user | ||
|
|
78 | + user = authenticator.authenticate(login, password) | ||
|
|
79 | + end | ||
|
|
80 | + end | ||
|
|
81 | + return user | ||
|
|
82 | + end | ||
|
|
83 | + end | ||
|
|
84 | + | ||
|
63 | end |
|
85 | end |
@@ -28,3 +28,6 | |||||
|
28 |
|
28 | ||
|
29 | # Uncomment so that configuration is read only once when the server is loaded |
|
29 | # Uncomment so that configuration is read only once when the server is loaded |
|
30 | # CONFIGURATION_CACHE_ENABLED = true |
|
30 | # CONFIGURATION_CACHE_ENABLED = true |
|
|
31 | + | ||
|
|
32 | + # Uncomment to allow authentication and user import from programming.in.th | ||
|
|
33 | + # LoginController.add_authenticator(ProgrammingAuthenticator.new) |
You need to be logged in to leave comments.
Login now