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
c5640dd9
Commit
c5640dd9
authored
Aug 11, 2017
by
liyijie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add oauth relation with token
parent
1e9cf58f
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
0 deletions
+32
-0
auth_token.rb
lib/rails_api_authentication/auth_token.rb
+0
-0
authable.rb
lib/rails_api_authentication/authable.rb
+15
-0
oauth_session.rb
lib/rails_api_authentication/oauth_session.rb
+17
-0
No files found.
lib/rails_api_authentication/auth_token.rb
View file @
c5640dd9
lib/rails_api_authentication/authable.rb
View file @
c5640dd9
...
@@ -73,10 +73,25 @@ module RailsApiAuthentication
...
@@ -73,10 +73,25 @@ module RailsApiAuthentication
end
end
def
oauth_login
(
oauth_type
,
oauth_id
)
def
oauth_login
(
oauth_type
,
oauth_id
)
if
@oauth_only
.
present?
user
=
self
.
find_or_create_by
oauth_type:
oauth_type
,
oauth_id:
oauth_id
user
=
self
.
find_or_create_by
oauth_type:
oauth_type
,
oauth_id:
oauth_id
AuthToken
.
create
(
AuthToken
.
create
(
self
,
{
oid:
user
.
id
,
oauth_type:
oauth_type
,
oauth_id:
oauth_id
}
self
,
{
oid:
user
.
id
,
oauth_type:
oauth_type
,
oauth_id:
oauth_id
}
)
)
else
auth
=
AuthToken
.
find
(
oauth_type:
oauth_type
,
oauth_id:
oauth_id
)
&
.
first
user
=
self
.
find_by
(
id:
auth
.
oid
)
raise
(
UserError
.
new
(
401
,
'-1'
,
'Unauthorized'
))
unless
user
.
present?
end
end
def
oauth_relate
(
token
,
oauth_type
,
oauth_id
)
auth
=
AuthToken
.
find
(
token:
token
)
&
.
first
if
auth
.
present?
&&
self
.
find_by
(
id:
auth
.
oid
).
present?
auth
.
update
(
oauth_type:
oauth_type
,
oauth_id:
oauth_id
)
else
raise
(
UserError
.
new
(
401
,
'-1'
,
'Unauthorized'
))
unless
user
.
present?
end
end
end
def
auth!
(
request
)
def
auth!
(
request
)
...
...
lib/rails_api_authentication/oauth_session.rb
View file @
c5640dd9
...
@@ -12,11 +12,28 @@ module RailsApiAuthentication
...
@@ -12,11 +12,28 @@ module RailsApiAuthentication
render
json:
{
error:
e
.
message
},
status:
e
.
status
render
json:
{
error:
e
.
message
},
status:
e
.
status
end
end
def
update
@auth_token
=
self
.
class
.
klass
.
oauth_relate
(
session_relate_params
.
delete
(
:token
),
session_relate_params
.
delete
(
:oauth_type
),
session_relate_params
.
delete
(
:oauth_id
),
)
render
json:
{
token:
@auth_token
.
token
},
status:
200
rescue
UserError
=>
e
render
json:
{
error:
e
.
message
},
status:
e
.
status
end
def
destroy
def
destroy
self
.
send
(
"current_
#{
self
.
class
.
klass_sym
}
"
)
&
.
logout
self
.
send
(
"current_
#{
self
.
class
.
klass_sym
}
"
)
&
.
logout
render
json:
{
message:
"logout successful"
},
status:
200
render
json:
{
message:
"logout successful"
},
status:
200
end
end
def
session_relate_params
params
.
require
(
self
.
class
.
klass_sym
).
permit
(
:oauth_type
,
:oauth_id
,
:token
)
end
def
session_params
def
session_params
params
.
require
(
self
.
class
.
klass_sym
).
permit
(
:oauth_type
,
:oauth_id
)
params
.
require
(
self
.
class
.
klass_sym
).
permit
(
:oauth_type
,
:oauth_id
)
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