# 文档(Apple授权登录)
https://developer.apple.com/documentation/signinwithapplerestapi
# 获取公钥Key接口
https://appleid.apple.com/auth/keys
# 验证客户端发送的authorizationCode接口
https://appleid.apple.com/auth/token
require 'net/https' require 'jwt' class AppleAuth APPLE_ISSUER = 'https://appleid.apple.com'.freeze APPLE_CONFIG = { client_id: ENV['CLIENT_ID'], # com.mytest.app key_id: ENV['KEY_ID'], # 10位字符 team_id: ENV['TEAM_ID'], # 10位字符 # Apple官方配置好下载下来文件内容 private_key: ENV['PRIVATE_KEY'] # -----BEGIN PRIVATE KEY----- *** } class << self def public_keys # 获取公钥keys uri = URI(File.join(APPLE_ISSUER, '/auth/keys')) res = Net::HTTP.get_response(uri) if res.code.to_i == 200 JSON.parse(res.body)['keys'] end end def jwt_token_info(jwt_token, key, verify, opts = {}) payload, header = JWT.decode(jwt_token, key, verify, opts) {payload: payload, header: header}.stringify_keys end end # token:identityToken # auth_code:authorizationCode # opts.email 客户端email # opts.apple_uid: userId 信息 # opts.idfa_str: IDFAStr 信息 # opts.family_name # opts.given_name def initialize(token, auth_code, opts = {}) @client_token = token @auth_code = auth_code @opts = opts.stringify_keys end # JWT 验证方式 def jwt_verify?(opts = {}) valid_token_info(opts).present? end def client_token_info # 得到Token内容时没有验证 @_client_token_info = self.class.jwt_token_info(@client_token, nil, false) end def valid_token_info(opts = {}) # 验证通过得到Token内容 opts.merge!(algorithm: client_token_info.dig('header', 'alg')) self.class.jwt_token_info(@client_token, public_key.keypair, true, opts) rescue {} end def auth_token_info # 通过/auth/token获取的id_token解析的信息 res = cache_apple_code_auth self.class.jwt_token_info(res['id_token'], nil, false) end private def public_key(force = false) # 获取解析Token的对应公钥 if @_public_key && !force @_public_key else if (_keys = self.class.public_keys) key = _keys.find { |obj| obj['kid'] == client_token_info.dig('header', 'kid') } @_public_key = JWT::JWK.import(key.symbolize_keys) if key end end end def code_verify_params # 构建请求/auth/token的参数 { grant_type: 'authorization_code', client_id: APPLE_CONFIG[:client_id], client_secret: client_secret, code: @auth_code, } end def client_secret payload = { iss: APPLE_CONFIG[:team_id], iat: Time.current.to_i, exp: 10.minutes.after.to_i, aud: APPLE_ISSUER, sub: APPLE_CONFIG[:client_id] } header = { # alg: 'ES256', 加上请求失败 "error"=>"invalid_request" kid: APPLE_CONFIG[:key_id] } ecdsa_key = OpenSSL::PKey::EC.new(APPLE_CONFIG[:private_key]) JWT.encode(payload, ecdsa_key, 'ES256', header) end def cache_apple_code_auth(force = false) uri = URI(File.join(APPLE_ISSUER, '/auth/token')) res = Net::HTTP.post_form(uri, code_verify_params) if res.code == 200 JSON.parse(res.body) else {} end end end
相关推荐
使用Rails构建可伸缩和可维护API的最佳方法
rails文档 rails api 英文
Rails 4 上的 Magento REST API Magento 在 Rails 4 上的 REST API 的一个简单示例,其中还包括基准测试 + oauth 注释。安装克隆这个 repo 运行bundle install 运行rails server配置您需要修改的唯一文件是settings/...
应用Rails进行REST开发.pdf Restful Rails Development
Ruby on Rails是一个突然流行...本文介绍Rails中的Web服务,重点放在一个名为Representational State Transfer (REST)的策略上。本文介绍了如何在Ruby on Rails中添加REST风格的Web服务,并从Ruby和Java代码调用服务。
rails帮助文档API
We still start with a step-by-step walkthrough of building a real application, and in-depth chapters look at the built-in Rails features. This edition now gives new Ruby and Rails users more ...
ruby on rails api方便查阅
构建Perfect Rails 5 API Only应用得益于作为Rails 5核心一部分提供的新的rails-api gem,Rails现在是快速,轻松构建精简API的理想选择。 到目前为止,可以说,在Ruby中创建API的最佳选择是Grape,尽管Grape仍然是一...
rails 2.2.2 API 英文的
个人收藏的rails api
rails的api文档,方便你查看
安装将google_sign_in添加到您的Rails应用的Gemfile并运行bundle install : gem 'google_sign_in' 要使用Rails进行Google登录,需要安装Rails 5.2或更高版本。组态首先,在Google API控制台中设置OAuth 2.0客户端ID...
Rails::API 是 Rails 的精简版本,针对不需要使用完整 Rails 功能的开发者。 Rails::API 移除了 ActionView 和其他一些渲染功能,不关心Web前端的开发者可更容易、快速地开发应用程序,因此运行速度比正常的 Rails ...
rails免费文档
Since the API documentation is liberally licensed (just like the rest of Rails), there are some sections of the book that draw from the API documentation. But in practically all of those cases, the ...
Rails3的ActiveRecord 查询API.doc
ruby on rails 2.3.5 api,自己留着用的,如果有人不知道怎么生成的话可以下载我生成的这个
Ruby.on.Rails.Tutorial,Learn.Web.Development.with.Rails,Third.Edition-中文版 文字版.pdf 个人收集电子书,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!