Skip to content

Commit

Permalink
Raise an error for :user token type with a login link inside
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWayfer committed Dec 4, 2020
1 parent 7538b8c commit b2dec1e
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 77 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,16 @@ You can pass nothing to `#check_tokens`, then client will generate new ones.
If you've specified `:token_type` as `:application` or have not specify it at all (default),
there will be an Application Access Token (without refresh token).

Otherwise, for User Access Token you will be asked to open a Twitch link in a browser
and login as user for whom tokens are intended.
Otherwise, for User Access Token here will be raised a `TwitchOAuth2::Error` with Twitch link
inside `#metadata[:link]`.

If you have a web-application with N users, you can redirect them to this link
and use `redirect_uri` to your application for callbacks.

Otherwise, if you have something like CLI tool, you can print instructions with a link for user.

Then you can use `#token(token_type: :user, code: 'a code from params in redirect uri')`
and get your `:access_token` and `:refresh_token`.

#### Reusing tokens

Expand Down
46 changes: 17 additions & 29 deletions lib/twitch_oauth2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,29 @@ def refreshed_tokens(refresh_token:)
refresh(refresh_token: refresh_token).slice(:access_token, :refresh_token)
end

private
def token(token_type:, code: nil)
response = CONNECTION.post(
'token',
client_id: @client_id,
client_secret: @client_secret,
code: code,
grant_type: grant_type_by_token_type(token_type),
redirect_uri: @redirect_uri
)

def flow(token_type:)
code = request_code if token_type == :user
return response.body if response.success?

token(code: code, token_type: token_type).slice(:access_token, :refresh_token)
raise Error, response.body[:message]
end

def request_code
link = authorize
private

puts <<~TEXT
1. Open URL in your browser:
#{link}
2. Login to Twitch.
3. Copy the `code` parameter from redirected URL.
4. Insert below:
TEXT
def flow(token_type:)
if token_type == :user
raise Error.new('Use `error.metadata[:link]` for getting new tokens', link: authorize)
end

$stdin.gets.chomp
token(token_type: token_type).slice(:access_token, :refresh_token)
end

def authorize
Expand All @@ -83,21 +86,6 @@ def authorize
raise Error, response.body[:message]
end

def token(code:, token_type:)
response = CONNECTION.post(
'token',
client_id: @client_id,
client_secret: @client_secret,
code: code,
grant_type: grant_type_by_token_type(token_type),
redirect_uri: @redirect_uri
)

return response.body if response.success?

raise Error, response.body[:message]
end

def grant_type_by_token_type(token_type)
case token_type
when :user then :authorization_code
Expand Down
8 changes: 8 additions & 0 deletions lib/twitch_oauth2/error.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# frozen_string_literal: true

module TwitchOAuth2
## Error during Twitch OAuth2 operations
class Error < StandardError
attr_reader :metadata

def initialize(message, metadata = {})
super message

@metadata = metadata
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
URI.decode_www_form(interaction.request.body).to_h['code']
end

config.filter_sensitive_data('<CODE>') do |interaction|
JSON.parse(interaction.request.body).to_h['code']
rescue JSON::ParserError
## this is not JSON
end

config.filter_sensitive_data('<ACCESS_TOKEN>') do |interaction|
if interaction.response.headers['content-type'].include? 'application/json'
JSON.parse(interaction.response.body)['access_token']
Expand Down
Loading

0 comments on commit b2dec1e

Please sign in to comment.