Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rails_dingtalk
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
other
rails_dingtalk
Commits
e0a69ec1
Commit
e0a69ec1
authored
Sep 05, 2021
by
mingyuan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
signrate
parent
54942dd8
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
103 additions
and
2 deletions
+103
-2
signature.rb
app/apis/dingtalk/signature.rb
+16
-0
apps_controller.rb
app/controllers/dingtalk/apps_controller.rb
+23
-1
dingtalk_user.rb
app/models/dingtalk/dingtalk_user.rb
+5
-0
new_app.rb
app/models/dingtalk/model/app/new_app.rb
+30
-0
oauth_user.rb
app/models/dingtalk/model/oauth_user.rb
+25
-0
routes.rb
config/routes.rb
+3
-0
docker
docker
+1
-1
No files found.
app/apis/dingtalk/signature.rb
0 → 100644
View file @
e0a69ec1
module
Dingtalk
module
Signature
extend
self
def
signature
(
secret
,
timestamp
=
(
Time
.
current
.
to_f
*
1000
).
round
)
raise
ArgumentError
,
'timestamp must in millis'
if
Math
.
log10
(
timestamp
).
ceil
<
13
origin_str
=
[
timestamp
,
secret
].
join
(
"
\n
"
)
signature_str
=
OpenSSL
::
HMAC
.
digest
(
'SHA256'
,
secret
,
origin_str
)
signature_str_base64
=
Base64
.
strict_encode64
(
signature_str
)
URI
.
encode_www_form_component
(
signature_str_base64
)
end
end
end
app/controllers/dingtalk/apps_controller.rb
View file @
e0a69ec1
module
Dingtalk
class
AppsController
<
BaseController
before_action
:set_app
,
only:
[
:info
]
before_action
:set_app
,
only:
[
:login
]
before_action
:set_app_by_corp
,
only:
[
:info
]
def
info
result
=
@app
.
xx
(
params
[
:code
])
render
json:
result
end
def
login
@dingtalk_user
=
@app
.
generate_user
(
params
[
:code
])
if
@oauth_user
.
account
.
nil?
&&
current_account
@oauth_user
.
account
=
current_account
end
@oauth_user
.
save
if
@oauth_user
.
user
login_by_oauth_user
(
@oauth_user
)
Com
::
SessionChannel
.
broadcast_to
(
params
[
:state
],
auth_token:
current_authorized_token
.
token
)
else
url_options
=
{}
url_options
.
merge!
params
.
except
(
:controller
,
:action
,
:id
,
:business
,
:namespace
,
:code
,
:state
).
permit!
url_options
.
merge!
host:
URI
(
session
[
:return_to
]).
host
if
session
[
:return_to
]
end
end
def
create
end
private
def
set_app
@app
=
App
.
find
params
[
:id
]
end
def
set_app_by_corp
@app
=
NormalApp
.
find_by
corp_id:
params
[
:corp_id
]
end
...
...
app/models/dingtalk/dingtalk_user.rb
0 → 100644
View file @
e0a69ec1
module
Dingtalk
class
DingtalkUser
<
ApplicationRecord
include
Model
::
DingtalkUser
end
end
app/models/dingtalk/model/app/new_app.rb
View file @
e0a69ec1
...
...
@@ -6,5 +6,35 @@ module Dingtalk
@api
=
Api
::
New
.
new
(
self
)
end
def
oauth2_url
(
scope
=
'openid corpid'
,
state:
SecureRandom
.
hex
(
16
),
**
host_options
)
h
=
{
client_id:
app_key
,
redirect_uri:
Rails
.
application
.
routes
.
url_for
(
controller:
'dingtalk/apps'
,
action:
'login'
,
id:
id
,
**
host_options
),
response_type:
'code'
,
scope:
scope
,
state:
state
,
nonce:
SecureRandom
.
hex
(
4
)
}
logger
.
debug
"
\e
[35m Detail:
#{
h
}
\e
[0m"
"https://login.dingtalk.com/oauth2/auth?
#{
h
.
to_query
}
"
end
def
generate_user
(
code
)
h
=
{
clientId:
app_key
,
clientSecret:
app_secret
,
code:
code
,
grantType:
'authorization_code'
}
r
=
HTTPX
.
post
"https://api.dingtalk.com/v1.0/oauth2/userAccessToken"
,
body:
h
.
to_json
result
=
JSON
.
parse
(
r
.
body
.
to_s
)
#binding.break
wechat_user
=
wechat_users
.
find_or_initialize_by
(
uid:
result
[
'openid'
])
wechat_user
.
assign_attributes
result
.
slice
(
'access_token'
,
'refresh_token'
,
'unionid'
)
wechat_user
.
expires_at
=
Time
.
current
+
result
[
'expires_in'
].
to_i
wechat_user
end
end
end
app/models/dingtalk/model/oauth_user.rb
0 → 100644
View file @
e0a69ec1
module
Dingtalk
module
Model::OauthUser
extend
ActiveSupport
::
Concern
included
do
attribute
:uid
,
:string
attribute
:unionid
,
:string
,
index:
true
attribute
:appid
,
:string
attribute
:name
,
:string
attribute
:avatar_url
,
:string
attribute
:state
,
:string
attribute
:access_token
,
:string
attribute
:expires_at
,
:datetime
attribute
:refresh_token
,
:string
attribute
:extra
,
:json
,
default:
{}
attribute
:identity
,
:string
,
index:
true
index
[
:uid
,
:provider
],
unique:
true
validates
:provider
,
presence:
true
validates
:uid
,
presence:
true
end
end
end
config/routes.rb
View file @
e0a69ec1
...
...
@@ -12,6 +12,9 @@ Rails.application.routes.draw do
collection
do
post
:info
end
member
do
get
:login
end
end
end
...
...
docker
@
db4479ca
Subproject commit
3016f0d02b3405166c033b20a6cb8e0ef27b60b8
Subproject commit
db4479ca828783fe8203f0867ed082dd6db8e0a6
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