Added files
This commit is contained in:
parent
3f1abb701b
commit
c45355aa0b
11 changed files with 221 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
.bundle
|
||||||
|
*.swp
|
||||||
|
.gem
|
||||||
|
Gemfile.lock
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/omniauth-weibo-oauth2.iml" filepath="$PROJECT_DIR$/.idea/omniauth-weibo-oauth2.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
19
.idea/omniauth-weibo-oauth2.iml
Normal file
19
.idea/omniauth-weibo-oauth2.iml
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="RUBY_MODULE" version="4">
|
||||||
|
<component name="ModuleRunConfigurationManager">
|
||||||
|
<shared />
|
||||||
|
</component>
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="ruby-3.2.3-p157" jdkType="RUBY_SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="bundler (v2.5.6, ruby-3.2.3-p157) [gem]" level="application" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="growl (v1.0.3, ruby-3.2.3-p157) [gem]" level="application" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="rake (v13.2.1, ruby-3.2.3-p157) [gem]" level="application" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="rb-fsevent (v0.11.2, ruby-3.2.3-p157) [gem]" level="application" />
|
||||||
|
</component>
|
||||||
|
</module>
|
13
Gemfile
Normal file
13
Gemfile
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
source 'https://rubygems.org'
|
||||||
|
|
||||||
|
# Specify your gem's dependencies in omniauth-weibo-oauth2.gemspec
|
||||||
|
gemspec
|
||||||
|
|
||||||
|
group :development, :test do
|
||||||
|
gem 'guard'
|
||||||
|
gem 'guard-rspec'
|
||||||
|
gem 'guard-bundler'
|
||||||
|
gem 'rb-fsevent'
|
||||||
|
gem 'growl'
|
||||||
|
gem 'rake'
|
||||||
|
end
|
21
LICENSE.txt
Normal file
21
LICENSE.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2024 Toastie
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
7
Rakefile
Normal file
7
Rakefile
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#!/usr/bin/env rake
|
||||||
|
require File.join('bundler', 'gem_tasks')
|
||||||
|
require File.join('rspec', 'core', 'rake_task')
|
||||||
|
|
||||||
|
RSpec::Core::RakeTask.new(:spec)
|
||||||
|
|
||||||
|
task :default => :spec
|
2
lib/omniauth-weibo-oauth2.rb
Normal file
2
lib/omniauth-weibo-oauth2.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
require "omniauth-weibo-oauth2/version"
|
||||||
|
require "omniauth/strategies/weibo"
|
5
lib/omniauth-weibo-oauth2/version.rb
Normal file
5
lib/omniauth-weibo-oauth2/version.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module OmniAuth
|
||||||
|
module WeiboOauth2
|
||||||
|
VERSION = "0.5.2"
|
||||||
|
end
|
||||||
|
end
|
113
lib/omniauth/strategies/weibo.rb
Normal file
113
lib/omniauth/strategies/weibo.rb
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
require "omniauth-oauth2"
|
||||||
|
|
||||||
|
module OmniAuth
|
||||||
|
module Strategies
|
||||||
|
class Weibo < OmniAuth::Strategies::OAuth2
|
||||||
|
option :client_options, {
|
||||||
|
:site => "https://api.weibo.com",
|
||||||
|
:authorize_url => "/oauth2/authorize",
|
||||||
|
:token_url => "/oauth2/access_token",
|
||||||
|
:token_method => :post
|
||||||
|
}
|
||||||
|
option :token_params, {
|
||||||
|
:parse => :json
|
||||||
|
}
|
||||||
|
|
||||||
|
uid do
|
||||||
|
raw_info['id']
|
||||||
|
end
|
||||||
|
|
||||||
|
info do
|
||||||
|
{
|
||||||
|
:nickname => raw_info['screen_name'],
|
||||||
|
:name => raw_info['name'],
|
||||||
|
:location => raw_info['location'],
|
||||||
|
:image => image_url,
|
||||||
|
:description => raw_info['description'],
|
||||||
|
:urls => {
|
||||||
|
'Blog' => raw_info['url'],
|
||||||
|
'Weibo' => raw_info['domain'].empty? ? "https://weibo.com/u/#{raw_info['id']}" : "https://weibo.com/#{raw_info['domain']}",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
extra do
|
||||||
|
{
|
||||||
|
:raw_info => raw_info
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def callback_url
|
||||||
|
token_params_redirect || (full_host + script_name + callback_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
def token_params_redirect
|
||||||
|
token_params['redirect_uri'] || token_params[:redirect_uri]
|
||||||
|
end
|
||||||
|
|
||||||
|
def raw_info
|
||||||
|
access_token.options[:mode] = :query
|
||||||
|
access_token.options[:param_name] = 'access_token'
|
||||||
|
@uid ||= access_token.get('/2/account/get_uid.json').parsed["uid"]
|
||||||
|
@raw_info ||= access_token.get("/2/users/show.json", :params => {:uid => @uid}).parsed
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_image
|
||||||
|
raw_info[%w(avatar_hd avatar_large profile_image_url).find { |e| raw_info[e].present? }]
|
||||||
|
end
|
||||||
|
|
||||||
|
#url: option: size:
|
||||||
|
#avatar_hd original original_size
|
||||||
|
#avatar_large large 180x180
|
||||||
|
#profile_image_url middle 50x50
|
||||||
|
# small 30x30
|
||||||
|
#default is middle
|
||||||
|
def image_url
|
||||||
|
image_size = options[:image_size] || :middle
|
||||||
|
case image_size.to_sym
|
||||||
|
when :original
|
||||||
|
url = raw_info['avatar_hd']
|
||||||
|
when :large
|
||||||
|
url = raw_info['avatar_large']
|
||||||
|
when :small
|
||||||
|
url = raw_info['avatar_large'].sub('/180/','/30/')
|
||||||
|
else
|
||||||
|
url = raw_info['profile_image_url']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# You can pass +display+, +with_offical_account+ or +state+ params to the auth request, if
|
||||||
|
# you need to set them dynamically. You can also set these options
|
||||||
|
# in the OmniAuth config :authorize_params option.
|
||||||
|
#
|
||||||
|
# /auth/weibo?display=mobile&with_offical_account=1
|
||||||
|
#
|
||||||
|
def authorize_params
|
||||||
|
super.tap do |params|
|
||||||
|
%w[display with_offical_account forcelogin state].each do |v|
|
||||||
|
if request.params[v]
|
||||||
|
params[v.to_sym] = request.params[v]
|
||||||
|
end
|
||||||
|
session["omniauth.state"] = params[v.to_sym] if v == 'state'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
def build_access_token
|
||||||
|
params = {
|
||||||
|
'client_id' => client.id,
|
||||||
|
'client_secret' => client.secret,
|
||||||
|
'code' => request.params['code'],
|
||||||
|
'grant_type' => 'authorization_code',
|
||||||
|
'redirect_uri' => callback_url
|
||||||
|
}.merge(token_params.to_hash(symbolize_keys: true))
|
||||||
|
client.get_token(params, deep_symbolize(options.token_params))
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
OmniAuth.config.add_camelization "weibo", "Weibo"
|
21
omniauth-weibo-oauth2.gemspec
Normal file
21
omniauth-weibo-oauth2.gemspec
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
require File.expand_path('../lib/omniauth-weibo-oauth2/version', __FILE__)
|
||||||
|
|
||||||
|
Gem::Specification.new do |gem|
|
||||||
|
gem.authors = "Bin He"
|
||||||
|
gem.email = "beenhero@gmail.com"
|
||||||
|
gem.description = %q{OmniAuth Oauth2 strategy for weibo.com.}
|
||||||
|
gem.summary = %q{OmniAuth Oauth2 strategy for weibo.com.}
|
||||||
|
gem.homepage = "https://github.com/beenhero/omniauth-weibo-oauth2"
|
||||||
|
gem.license = "MIT"
|
||||||
|
|
||||||
|
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
||||||
|
gem.files = `git ls-files`.split("\n")
|
||||||
|
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
||||||
|
gem.name = "omniauth-weibo-oauth2"
|
||||||
|
gem.require_paths = ["lib"]
|
||||||
|
gem.version = OmniAuth::WeiboOauth2::VERSION
|
||||||
|
|
||||||
|
gem.add_dependency 'omniauth', '~> 1.5'
|
||||||
|
gem.add_dependency 'omniauth-oauth2', '>= 1.4.0'
|
||||||
|
end
|
Loading…
Reference in a new issue