Commit e0a69ec1 by mingyuan

signrate

parent 54942dd8
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
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
......
module Dingtalk
class DingtalkUser < ApplicationRecord
include Model::DingtalkUser
end
end
......@@ -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
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
......@@ -12,6 +12,9 @@ Rails.application.routes.draw do
collection do
post :info
end
member do
get :login
end
end
end
......
Subproject commit 3016f0d02b3405166c033b20a6cb8e0ef27b60b8
Subproject commit db4479ca828783fe8203f0867ed082dd6db8e0a6
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment