Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rails_api_authentication
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
rails_api_authentication
Commits
9e9658eb
Commit
9e9658eb
authored
Aug 02, 2017
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add code session controller module
parent
b2fa073d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
2 deletions
+49
-2
rails_api_authentication.rb
lib/rails_api_authentication.rb
+1
-0
acts_as_authentication_handler.rb
...ails_api_authentication/acts_as_authentication_handler.rb
+7
-0
authable.rb
lib/rails_api_authentication/authable.rb
+4
-2
code_session.rb
lib/rails_api_authentication/code_session.rb
+37
-0
No files found.
lib/rails_api_authentication.rb
View file @
9e9658eb
...
...
@@ -2,6 +2,7 @@ require "rails_api_authentication/version"
require
"rails_api_authentication/configuration"
require
"rails_api_authentication/auth_action"
require
"rails_api_authentication/auth_session"
require
"rails_api_authentication/code_session"
require
"rails_api_authentication/auth_password"
require
"rails_api_authentication/auth_token"
require
"rails_api_authentication/authable"
...
...
lib/rails_api_authentication/acts_as_authentication_handler.rb
View file @
9e9658eb
...
...
@@ -18,5 +18,12 @@ module RailsApiAuthentication
auth_action
klass_sym
,
only:
[
:update
]
auth_password
klass_sym
end
def
acts_as_code_session
(
klass_sym
)
include
RailsApiAuthentication
::
AuthAction
include
RailsApiAuthentication
::
CodeSession
auth_action
klass_sym
,
only:
[
:destroy
]
code_session
klass_sym
end
end
end
lib/rails_api_authentication/authable.rb
View file @
9e9658eb
...
...
@@ -50,7 +50,7 @@ module RailsApiAuthentication
end
def
code_login
name
,
code
raise
(
UserError
.
new
(
401
,
'-1'
,
"The authorization need password"
))
if
@auth_password
.
present
raise
(
UserError
.
new
(
401
,
'-1'
,
"The authorization need password"
))
if
@auth_password
.
present
?
valid!
name
,
code
user
=
self
.
find_or_create_by
(
@auth_key
=>
name
)
raise
(
UserError
.
new
(
401
,
'-1'
,
'Unauthorized'
))
if
user
.
nil?
...
...
@@ -76,7 +76,9 @@ module RailsApiAuthentication
def
register
(
name
,
password
,
attrs
=
{})
raise
(
UserError
.
new
(
401
,
'-1'
,
'password is blank'
))
if
password
.
blank?
valid!
name
,
attrs
.
delete
(
@valid_key
)
self
.
create!
({
@auth_key
=>
name
,
@auth_password
=>
generate_password
(
password
)}.
merge
attrs
)
user
=
self
.
create!
({
@auth_key
=>
name
,
@auth_password
=>
generate_password
(
password
)}.
merge
attrs
)
user
.
token
=
AuthToken
.
create
(
self
,
{
oid:
user
.
id
}).
token
user
rescue
ActiveRecord
::
RecordInvalid
=>
e
raise
UserError
.
new
(
401
,
'-1'
,
e
.
message
)
end
...
...
lib/rails_api_authentication/code_session.rb
0 → 100644
View file @
9e9658eb
module
RailsApiAuthentication
module
CodeSession
extend
ActiveSupport
::
Concern
included
do
end
def
create
auth_key
=
self
.
class
.
klass
.
auth_key
valid_key
=
self
.
class
.
klass
.
valid_key
@auth_token
=
self
.
class
.
klass
.
code_login
(
session_params
[
auth_key
],
session_params
[
valid_key
])
render
json:
{
token:
@auth_token
.
token
},
status:
200
rescue
UserError
=>
e
render
json:
{
error:
e
.
message
},
status:
e
.
status
end
def
destroy
self
.
send
(
"current_
#{
self
.
class
.
klass_sym
}
"
)
&
.
logout
render
json:
{
message:
"logout successful"
},
status:
200
end
private
def
session_params
auth_key
=
self
.
class
.
klass
.
auth_key
valid_key
=
self
.
class
.
klass
.
valid_key
params
.
require
(
self
.
class
.
klass_sym
).
permit
(
auth_key
,
valid_key
)
end
module
ClassMethods
attr_reader
:klass
,
:klass_sym
def
code_session
klass_sym
@klass
=
klass_sym
.
to_s
.
camelize
.
constantize
@klass_sym
=
klass_sym
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment