Repository navigation
Ruby SDK: a token provider resent once on a 401, and ~/.loom/token #1697
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7b2c351
Ruby SDK: a token provider resent once on a 401, and ~/.loom/token
rahulsjha 9e09fa4
Ruby SDK: freeze the base URL the token provider's origin was checked…
rahulsjha 021556d
Ruby SDK: check the token provider's origin before each token is sent
rahulsjha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "uri" | ||
|
|
||
| module ClickHouse | ||
| module Loom | ||
| # Where a Client's bearer token comes from, beyond a static API key. | ||
| # | ||
| # A +token_provider+ is any object with +token(refused)+ (or a Proc taking +refused+) | ||
| # that returns the token to send, as a String. The client asks it before every | ||
| # request. After a 401 it asks once more, passing the token the server refused in | ||
| # +refused+, and sends the call again with the answer; a provider that has no fresh | ||
| # token returns the refused one, and the 401 is raised. An empty token sends no | ||
| # Authorization header. A provider that responds to +origin+ ("https://loom.example") | ||
| # is only ever used with a client for that origin. This is the TypeScript SDK's | ||
| # TokenProvider: a bot's machine login or a stored `loom login` session plugs in here. | ||
| # | ||
| # The last credential a client looks for is the first line of ~/.loom/token, which | ||
| # `loom mint-token --save` writes, as the TypeScript and Python SDKs read it. | ||
| module Credentials | ||
| # The file `loom mint-token --save` writes, under the user's home directory. | ||
| TOKEN_FILE = File.join("~", ".loom", "token") | ||
|
|
||
| module_function | ||
|
|
||
| # The first line of ~/.loom/token (or +path+), stripped; nil when the file is | ||
| # missing, unreadable or blank, or no home directory is known. | ||
| def token_from_file(path = nil) | ||
| text = File.read(File.expand_path(path || TOKEN_FILE), encoding: "UTF-8") | ||
| line = text.strip.lines.first.to_s.strip | ||
| line.empty? ? nil : line | ||
| rescue SystemCallError, IOError, ArgumentError, EncodingError | ||
| nil | ||
| end | ||
|
|
||
| # The provider's token, with +refused+ the token the server just answered 401 to | ||
| # (nil on a first try). | ||
| def provider_token(provider, refused) | ||
| token = | ||
| if provider.respond_to?(:token) | ||
| provider.token(refused) | ||
| elsif provider.respond_to?(:call) | ||
| provider.arity.zero? ? provider.call : provider.call(refused) | ||
| else | ||
| raise ArgumentError, "token_provider must respond to token(refused) or call(refused)" | ||
| end | ||
| token.to_s.strip | ||
| end | ||
|
|
||
| # (scheme, host, port) of +url+, as a browser compares origins. | ||
| def origin_of(url) | ||
| uri = URI(url.to_s) | ||
| [uri.scheme.to_s.downcase, uri.host.to_s.downcase, uri.port] | ||
| rescue URI::InvalidURIError | ||
| nil | ||
| end | ||
|
|
||
| # Raises ArgumentError when +provider+ is bound to another origin than +base_url+: | ||
| # its token is never sent to a server it was not made for. | ||
| def check_origin!(provider, base_url) | ||
| return unless provider.respond_to?(:origin) && provider.origin | ||
|
|
||
| bound = origin_of(provider.origin) | ||
| return if bound && bound == origin_of(base_url) | ||
|
|
||
| label = provider.respond_to?(:label) && provider.label ? provider.label : "the token provider" | ||
| raise ArgumentError, "refusing to send #{label}'s token to #{URI(base_url).host}: it is bound to " \ | ||
| "#{provider.origin}" | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.