PHP服务端如何进行苹果登录的验证

一、材料准备

1、P8文件:苹果后台生成证书那里的key那里生成,这个文件只可以下载一次,保存好

2、生成JWT token 的脚本

二、脚本siwa.rb

ruby 复制代码
#!/usr/bin/env ruby

require 'jwt'
require 'openssl'
require 'optparse'

options = {}

OptionParser.new do |opts|
  opts.banner = "Usage: siwa.rb [options]"

  opts.on("-k", "--key PATH", "Path to the AuthKey file") do |v|
    options[:key] = v
  end
  opts.on("-t", "--team-id TEAM_ID", "Your Apple Team ID") do |v|
    options[:team_id] = v
  end
  opts.on("-b", "--bundle-id BUNDLE_ID", "Your App's Bundle ID") do |v|
    options[:bundle_id] = v
  end
  opts.on("-h", "--help", "Prints this help") do
    puts opts
    exit
  end
end.parse!

if options[:key].nil? || options[:team_id].nil? || options[:bundle_id].nil?
  puts OptionParser.new.help
  exit
end

pem_content = File.read options[:key]

ecdsa_key = OpenSSL::PKey::EC.new pem_content

headers = {
    'kid' => options[:team_id],
}

claims = {
    'iss' => options[:team_id],
    'iat' => Time.now.to_i,
    'exp' => Time.now.to_i + 86400*180,
    'aud' => 'https://appleid.apple.com',
    'sub' => options[:bundle_id],
}

token = JWT.encode claims, ecdsa_key, 'ES256', headers

puts token

三、运行生成token

1、把脚本放在桌面,命令行执行:chmod +x /Users/say/Desktop/siwa.rb

2、cd到桌面,运行:./siwa.rb -h

如果报错:

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- jwt ( LoadError )

from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'

from ./siwa.rb:3:in `<main>'

说明电脑上没有JWT

3、安装JWT:sudo gem install jwt

4、执行:./siwa.rb -h

终端会输出:

Usage: siwa.rb [options]

-k, --key PATH Path to the AuthKey file

-t, --team-id TEAM_ID Your Apple Team ID

-b, --bundle-id BUNDLE_ID Your App's Bundle ID

-h, --help Prints this help

5、运行:./siwa.rb -k /Users/5T3.p8 -t 684MCOO9T3 -b com.kcys.sx

就会生成JWT 字符串

相关推荐
BingoGo6 小时前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack6 小时前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo1 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack1 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack2 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo2 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack3 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端