tiktok-oauth-strategy 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a63dcfcd8cad661a3223322fcaea5761da061cdf0175f1a20a8dbc698ad8628c
4
+ data.tar.gz: ab727a28724c7c5ff8a5b2d6df4b368affecbb66abf736491a3bb7cd87519d17
5
+ SHA512:
6
+ metadata.gz: 79d3c61cb27c2773c0b0b5e82c805d5c690045c9b43782fae7d7dce49fec2d9c995eae43da07fb13b0ff52d125b4a859d07065d71ee1652954cacdb70379e1db
7
+ data.tar.gz: 8e7195bd62130d444781983616ea3f1afdc174ce5532e69db72ef383d3812a6331b6803cc7f0c287a9c74208c224fc41f5e628d33b3182d8c11952ac06282719
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'rubocop', '>= 0.48.1'
5
+ end
6
+
7
+ group :test do
8
+ gem 'webmock', '~> 3.13'
9
+ gem 'rack-test'
10
+ gem 'rspec', '~> 3.6.0' # '~> 3.6.0'
11
+ gem 'simplecov', require: false
12
+ end
13
+
14
+ gemspec
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # tiktok_oauth_strategy
2
+
3
+ An unofficial OmniAuth strategy for TikTok authentication with the [TikTok Login Kit](https://developers.tiktok.com/doc/login-kit-web/).
4
+
5
+
6
+ ## Usage
7
+
8
+ Install the gem
9
+
10
+ ```ruby
11
+ gem 'tiktok_oauth_strategy'
12
+ bundle install
13
+ ```
14
+
15
+ Make a TikTok developer account and create an application. Retrieve the app's `TIKTOK_CLIENT_ID` and `TIKTOK_CLIENT_SECRET` once it is approved and add a middleware to your ruby project, typically in a config/initializers/omniauth.rb file :
16
+
17
+ ```ruby
18
+ Rails.application.config.middleware.use OmniAuth::Builder do
19
+ provider :tiktok, ENV['TIKTOK_CLIENT_ID'], ENV['TIKTOK_CLIENT_SECRET']
20
+ end
21
+ ```
22
+
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Tiktok < OmniAuth::Strategies::OAuth2
4
+ option :name, "tiktok"
5
+ option :response_type, "code"
6
+
7
+ uid { raw_info["open_id"] }
8
+
9
+ extra do
10
+ {
11
+ raw_info: raw_info
12
+ }
13
+ end
14
+
15
+ info do
16
+ {
17
+ name: raw_info["display_name"],
18
+ description: raw_info["description"],
19
+ image: raw_info["avatar"],
20
+ large_image: raw_info["avatar_larger"],
21
+ secondary_social_id: raw_info["union_id"],
22
+ refresh_token_expiry: access_token["refresh_expires_in"].seconds.from_now,
23
+ access_token_expiry: Time.at(access_token.expires_at).utc.to_datetime
24
+ }
25
+ end
26
+
27
+ def client
28
+ # config/initializers/tiktok_oauth2.rb
29
+ ::TiktokOAuth2Client.new(options.client_id, options.client_secret, deep_symbolize(options.client_options))
30
+ end
31
+
32
+ # overriding method to pass in client_key to authorization url
33
+ def authorize_params
34
+ super.tap do |params|
35
+ params[:client_key] = options[:client_key]
36
+ session["omniauth.state"] = params[:state] if params[:state]
37
+ end
38
+ end
39
+
40
+ def raw_info
41
+ uri = "https://open-api.tiktok.com/oauth/userinfo/"
42
+
43
+ query_params = {
44
+ open_id: access_token.params['open_id'],
45
+ access_token: access_token.token
46
+ }
47
+
48
+ result = HTTParty.get(uri, query: query_params)
49
+ @raw_info ||= result.parsed_response["data"]
50
+ end
51
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Overriding a few methods for the OAuth2::Client (https://github.com/oauth-xx/oauth2/blob/422d3132343227cec84d5d34a7a8ce4825439bdb/lib/oauth2/client.rb)
4
+ #
5
+ # TikTok Returns it's access_token inside of a data attribute which is unexpected by OAuth2. Overriding methods to look inside of the data attribute for
6
+ # the access_token
7
+
8
+ class TiktokOAuth2Client < OAuth2::Client
9
+ # Initializes an AccessToken by making a request to the token endpoint
10
+ #
11
+ # @param params [Hash] a Hash of params for the token endpoint
12
+ # @param access_token_opts [Hash] access token options, to pass to the AccessToken object
13
+ # @param access_token_class [Class] class of access token for easier subclassing OAuth2::AccessToken
14
+ # @return [AccessToken] the initialized AccessToken
15
+ def get_token(params, access_token_opts = {}, access_token_class = OAuth2::AccessToken) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
16
+ params = params.map do |key, value|
17
+ if RESERVED_PARAM_KEYS.include?(key)
18
+ [key.to_sym, value]
19
+ else
20
+ [key, value]
21
+ end
22
+ end
23
+
24
+ params = authenticator.apply(params.to_h)
25
+ opts = { raise_errors: options[:raise_errors], parse: params.delete(:parse) }
26
+ headers = params.delete(:headers) || {}
27
+ if options[:token_method] == :post
28
+ opts[:body] = params
29
+ opts[:headers] = { "Content-Type" => "application/x-www-form-urlencoded" }
30
+ else
31
+ opts[:params] = params
32
+ opts[:headers] = {}
33
+ end
34
+ opts[:headers].merge!(headers)
35
+ http_method = options[:token_method] == :post_with_query_string ? :post : options[:token_method]
36
+ response = request(http_method, token_url, opts)
37
+ response_contains_token = response.parsed.is_a?(Hash) &&
38
+ (response.parsed.dig("data", "access_token") || response.parsed.dig("data", "id_token"))
39
+
40
+ raise OAuth2::Error, response if options[:raise_errors] && !response_contains_token
41
+
42
+ return nil if !response_contains_token
43
+
44
+ build_access_token(response, access_token_opts, access_token_class)
45
+ end
46
+
47
+ private
48
+
49
+ # Returns the authenticator object
50
+ #
51
+ # @return [OAuth2::Authenticator] the initialized Authenticator
52
+ def authenticator
53
+ OAuth2::Authenticator.new(id, secret, options[:auth_scheme])
54
+ end
55
+
56
+ # Builds the access token from the response of the HTTP call
57
+ #
58
+ # @return [AccessToken] the initialized AccessToken
59
+ def build_access_token(response, access_token_opts, access_token_class)
60
+ access_token_class.from_hash(self, response.parsed["data"].merge(access_token_opts)).tap do |access_token|
61
+ access_token.response = response if access_token.respond_to?(:response=)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module TiktokOAuthStrategy
2
+ VERSION = '0.0.0'
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'tiktok-oauth-strategy/version'
2
+ require 'omniauth/strategies/tiktok'
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/tiktok_oauth_strategy/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'tiktok-oauth-strategy'
7
+ gem.version = TiktokOAuthStrategy::VERSION
8
+ gem.authors = ['Fariha Hossain']
9
+ gem.email = ['fthossain220@gmail.com']
10
+ gem.description = 'OmniAuth strategy for TikTok.'
11
+ gem.summary = 'OmniAuth strategy for TikTok.'
12
+ gem.homepage = 'https://github.com/f-hossain/tiktok_oauth_strategy'
13
+
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.require_paths = ['lib']
16
+
17
+ gem.add_dependency 'omniauth', '~> 2.0'
18
+ gem.add_dependency 'omniauth-oauth2', '~> 1.7'
19
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiktok-oauth-strategy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Fariha Hossain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ description: OmniAuth strategy for TikTok.
42
+ email:
43
+ - fthossain220@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - README.md
50
+ - lib/strategies/tiktok.rb
51
+ - lib/strategies/tiktok_oauth2_client.rb
52
+ - lib/tiktok_oauth_strategy.rb
53
+ - lib/tiktok_oauth_strategy/version.rb
54
+ - tiktok_oauth_strategy.gemspec
55
+ homepage: https://github.com/f-hossain/tiktok_oauth_strategy
56
+ licenses: []
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.0.3.1
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: OmniAuth strategy for TikTok.
77
+ test_files: []