-
Notifications
You must be signed in to change notification settings - Fork 7
Adding option to explicitly set token, token_name on initialization #11
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
base: main
Are you sure you want to change the base?
Changes from all commits
a0440f2
fae9a11
14296d2
de48956
f979c25
c48079f
81e3947
01a17a5
03774d1
62627fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,6 @@ class NotConfiguredError < StandardError; end | |
| DEFAULT_CRITICAL_RANGE = 5000...100_000_000 | ||
|
|
||
| LENIENT_MODE_ENV_VARIABLE = "HACKERONE_CLIENT_LENIENT_MODE" | ||
|
|
||
| REPORT_STATES = %w( | ||
| new | ||
| triaged | ||
|
|
@@ -48,7 +47,7 @@ class NotConfiguredError < StandardError; end | |
|
|
||
| class << self | ||
| ATTRS = [:low_range, :medium_range, :high_range, :critical_range].freeze | ||
| attr_accessor :program | ||
| attr_accessor :program, :token, :token_name | ||
| attr_reader *ATTRS | ||
|
|
||
| ATTRS.each do |attr| | ||
|
|
@@ -60,14 +59,28 @@ class << self | |
| end | ||
|
|
||
| class Api | ||
| def initialize(program = nil) | ||
| def initialize(program = nil, token: nil, token_name: nil) | ||
| @program = program | ||
| @token = token || ENV["HACKERONE_TOKEN"] | ||
| @token_name = token_name || ENV["HACKERONE_TOKEN_NAME"] | ||
| # Set class-level token and token_name if provided | ||
| if token | ||
| HackerOne::Client.token = token | ||
| end | ||
|
|
||
| if token_name | ||
| HackerOne::Client.token_name = token_name | ||
| end | ||
| end | ||
|
|
||
| def program | ||
| @program || HackerOne::Client.program | ||
| end | ||
|
|
||
| def token_name | ||
| @token_name || ENV["HACKERONE_TOKEN_NAME"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would this be needed? As far as I can tell, this conditional would only ever evaluate if the value of the I suppose it would help from a backwards compatibility perspective, but just want to make sure this line earns its place in the code. |
||
| end | ||
|
|
||
| def reporters | ||
| raise ArgumentError, "Program cannot be nil" unless program | ||
| response = self.class.hackerone_api_connection.get do |req| | ||
|
|
@@ -200,12 +213,12 @@ def self.parse_response(response, extract_data: true) | |
| end | ||
|
|
||
| def self.hackerone_api_connection | ||
| unless ENV["HACKERONE_TOKEN_NAME"] && ENV["HACKERONE_TOKEN"] | ||
| raise NotConfiguredError, "HACKERONE_TOKEN_NAME HACKERONE_TOKEN environment variables must be set" | ||
| unless HackerOne::Client.token_name && HackerOne::Client.token | ||
| raise NotConfiguredError, "Either set token_name and token or HACKERONE_TOKEN_NAME and HACKERONE_TOKEN environment variables" | ||
| end | ||
|
|
||
| @connection ||= Faraday.new(url: "https://api.hackerone.com/v1") do |faraday| | ||
| faraday.request(:authorization, :basic, ENV["HACKERONE_TOKEN_NAME"], ENV["HACKERONE_TOKEN"]) | ||
| faraday.request(:authorization, :basic, HackerOne::Client.token_name, HackerOne::Client.token) | ||
| faraday.adapter Faraday.default_adapter | ||
| end | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ehhhhh, if we're setting this token at a class level then this is no better logically than if we just continue to use environment variables; part of the impetus to use tokens as variables is such that there is instance level encapsulation. This adds side effects across entire classes which defeats the purpose of this in the first place. This would clobber any calls to HackerOne::Client methods anytime after a new API object is created, which doesn't seem ideal. I think 🤔
It is also tough because that class is designed to operate as a class, which makes encapsulation really tough.
Some alternatives:
HackerOne::ClientrunningHackerOne::Cientinside theHackerOne::Apiobject, and operate on that object. This would certainly not be backwards compatible.HackerOne::Clientand all subclasses would only be able to respond to the environment variableHackerOne::Clientobject such that it becomes a class instead of an objectThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Appreciate the feedback ...
I think I prefer this too, but timeline changes with that. Gonna have to think on it a little more 🙇🏼