Skip to content

Fetch user info #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/omniauth/strategies/wechat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,19 @@ def request_phase
end

def raw_info
log(:info, "Access token: #{access_token.to_hash}")

@uid ||= access_token["openid"]
@raw_info ||= begin
access_token.options[:mode] = :query
if access_token["scope"] == "snsapi_userinfo"
response = access_token.get("/sns/userinfo", :params => {"openid" => @uid, "lang" => "zh_CN"}, parse: :text)
@raw_info = JSON.parse(response.body.gsub(/[\u0000-\u001f]+/, ''))
if access_token["scope"]&.include?("snsapi_login")
access_token.get("/sns/userinfo", :params => { "openid" => @uid, "lang" => "zh_CN" }, parse: :json).parsed
else
@raw_info = {"openid" => @uid }
@raw_info.merge!("unionid" => access_token["unionid"]) if access_token["unionid"]
@raw_info
{ "openid" => @uid }
end
end
log(:info, "raw_info: #{@raw_info}")
@raw_info
end

protected
Expand Down
53 changes: 28 additions & 25 deletions spec/omniauth/strategies/wechat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,41 +110,44 @@
end
end

context "when scope is snsapi_userinfo" do
context "when scope is snsapi_login" do
let(:access_token) { OAuth2::AccessToken.from_hash(client, {
"openid"=>"openid",
"scope"=>"snsapi_userinfo",
"scope"=>"snsapi_login",
"access_token"=>"access_token"
})}

specify "will query for user info" do
response_body = %({"openid": "OPENID","nickname": "\x14\x1fNICKNAME", "sex": "1", "province": "PROVINCE", "city": "CITY", "country": "COUNTRY", "headimgurl": "header_image_url", "privilege": ["PRIVILEGE1", "PRIVILEGE2"]})

response_hash = {
"openid" => "OPENID",
"nickname" => "NICKNAME",
"sex" => "1",
"province" => "PROVINCE",
"city" => "CITY",
"country" => "COUNTRY",
"headimgurl" => "header_image_url",
"privilege" => ["PRIVILEGE1", "PRIVILEGE2"]
}

client.should_receive(:request).with do |verb, path, opts|
expect(verb).to eq(:get)
expect(path).to eq("/sns/userinfo")
expect(opts[:params]).to eq("openid"=> "openid", "lang"=>"zh_CN", "access_token"=> "access_token")
expect(opts[:parse]).to eq(:text)
end.and_return(double("response", body: response_body))

expect(subject.raw_info).to eq(response_hash)
expect(opts[:parse]).to eq(:json)
end.and_return(double("response", parsed:
{
openid: "OPENID",
nickname: "NICKNAME",
sex: "1",
province: "PROVINCE",
city: "CITY",
country: "COUNTRY",
headimgurl: "header_image_url",
privilege: ["PRIVILEGE1", "PRIVILEGE2"]
}
))
expect(subject.raw_info).to eq(
{
openid: "OPENID",
nickname: "NICKNAME",
sex: "1",
province: "PROVINCE",
city: "CITY",
country: "COUNTRY",
headimgurl: "header_image_url",
privilege: ["PRIVILEGE1", "PRIVILEGE2"]
}
)
end

end

end



end
end