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
3d8622e8
Commit
3d8622e8
authored
Jul 26, 2017
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finish user password function
parent
90f08b80
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
20 additions
and
6 deletions
+20
-6
rails_api_authentication.rb
lib/rails_api_authentication.rb
+1
-0
acts_as_authentication_handler.rb
...ails_api_authentication/acts_as_authentication_handler.rb
+4
-0
auth_password.rb
lib/rails_api_authentication/auth_password.rb
+5
-2
authable.rb
lib/rails_api_authentication/authable.rb
+10
-4
No files found.
lib/rails_api_authentication.rb
View file @
3d8622e8
...
...
@@ -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/auth_password"
require
"rails_api_authentication/auth_token"
require
"rails_api_authentication/authable"
require
"rails_api_authentication/acts_as_authenticationable"
...
...
lib/rails_api_authentication/acts_as_authentication_handler.rb
View file @
3d8622e8
...
...
@@ -6,12 +6,16 @@ module RailsApiAuthentication
end
def
acts_as_auth_session
(
klass_sym
)
include
RailsApiAuthentication
::
AuthAction
include
RailsApiAuthentication
::
AuthSession
auth_action
klass_sym
,
only:
[
:destroy
]
auth_session
klass_sym
end
def
acts_as_auth_password
(
klass_sym
)
include
RailsApiAuthentication
::
AuthAction
include
RailsApiAuthentication
::
AuthPassword
auth_action
klass_sym
,
only:
[
:update
]
auth_password
klass_sym
end
end
...
...
lib/rails_api_authentication/auth_password.rb
View file @
3d8622e8
...
...
@@ -7,9 +7,11 @@ module RailsApiAuthentication
# Reset password with token
def
create
auth_key
=
self
.
class
.
klass
.
auth_key
auth_password
=
self
.
class
.
klass
.
auth_password
valid_key
=
self
.
class
.
klass
.
valid_key
self
.
send
(
"current_
#{
self
.
class
.
klass_sym
}
"
)
&
.
reset_password
(
reset_password_params
[
auth_password
],
reset_password_params
[
valid_key
])
current_authable
=
self
.
class
.
klass
.
send
(
:find_by!
,
auth_key
=>
reset_password_params
[
auth_key
])
current_authable
.
reset_password
(
reset_password_params
[
auth_password
],
reset_password_params
[
valid_key
])
render
json:
{
meesage:
"reset password successful"
},
status:
200
rescue
UserError
=>
e
render
json:
{
error:
e
.
message
},
status:
e
.
status
...
...
@@ -31,10 +33,11 @@ module RailsApiAuthentication
end
def
reset_password_params
auth_key
=
self
.
class
.
klass
.
auth_key
auth_password
=
self
.
class
.
klass
.
auth_password
valid_key
=
self
.
class
.
klass
.
valid_key
params
.
require
(
self
.
class
.
klass_sym
).
permit
(
auth_password
,
valid_key
auth_
key
,
auth_
password
,
valid_key
)
end
...
...
lib/rails_api_authentication/authable.rb
View file @
3d8622e8
...
...
@@ -13,11 +13,13 @@ module RailsApiAuthentication
def
update_password
password
raise
(
UserError
.
new
(
401
,
'-1'
,
'password is blank'
))
if
password
.
blank?
self
.
update
(
@auth_password
=>
generate_password
(
password
))
auth_password
=
self
.
class
.
auth_password
self
.
update
(
auth_password
=>
self
.
class
.
send
(
:generate_password
,
password
))
end
def
reset_password
password
,
valid_code
update_password
(
password
)
if
self
.
class
.
valid?
(
self
.
send
(
@auth_key
),
valid_code
)
auth_key
=
self
.
class
.
auth_key
update_password
(
password
)
if
self
.
class
.
valid!
(
self
.
send
(
auth_key
),
valid_code
)
end
end
...
...
@@ -31,7 +33,7 @@ module RailsApiAuthentication
def
valid_for
params
@valid_key
=
params
[
:key
]
&
.
to_sym
||
:valid_code
@valid_expire
=
params
[
:expire
]
&
.
to_sym
||
60
@valid_expire
=
params
[
:expire
]
&
.
to_sym
||
60
0
@valid_length
=
params
[
:length
]
&
.
to_sym
||
4
end
...
...
@@ -56,7 +58,7 @@ module RailsApiAuthentication
def
register
(
name
,
password
,
attrs
=
{})
raise
(
UserError
.
new
(
401
,
'-1'
,
'password is blank'
))
if
password
.
blank?
raise
(
UserError
.
new
(
401
,
'-1'
,
'valid token is not correct'
))
unless
valid?
(
name
,
attrs
.
delete
(
@valid_key
)
)
valid!
name
,
attrs
.
delete
(
@valid_key
)
self
.
create!
({
@auth_key
=>
name
,
@auth_password
=>
generate_password
(
password
)}.
merge
attrs
)
rescue
ActiveRecord
::
RecordInvalid
=>
e
raise
UserError
.
new
(
401
,
'-1'
,
e
.
message
)
...
...
@@ -69,6 +71,10 @@ module RailsApiAuthentication
register
(
name
,
password
,
attrs
)
end
def
valid!
name
,
valid_code
raise
(
UserError
.
new
(
401
,
'-1'
,
'valid token is not correct'
))
unless
valid?
(
name
,
valid_code
)
end
private
def
salt
(
password
,
suffix
)
...
...
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