Skip to content

Problem with svg token #1222

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

Open
wants to merge 24 commits into
base: karol-tmp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
try with svg_token (wip)
  • Loading branch information
karoltravis committed Jan 13, 2022
commit 541fdbe0361b639af34ce17a67e96d315db66806
15 changes: 15 additions & 0 deletions lib/travis/api/app/helpers/respond_with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module RespondWith
}

def respond_with(resource, options = {})
halt 403, 'access denied' unless token_proper?(options[:acceptable_tokens])

result = respond(resource, options)

if result && response.content_type =~ /application\/json/
Expand All @@ -32,6 +34,19 @@ def body(value = nil, options = {}, &block)
end

private
# def acceptable_tokens(responder)
# case(responder)
# when :atom
# else
# end

def token_proper?(acceptable_tokens)
return true unless params[:token] # it means that ScopeCheck granted access basing on other proper token

acceptable_tokens ||= [:default]
token = Token.find_by_token(params[:token])
acceptable_tokens.include?(token.try(:type))
end

def respond(resource, options)
resource = apply_service_responder(resource, options)
Expand Down
8 changes: 8 additions & 0 deletions lib/travis/model/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class Token < Travis::Model

serialize :token, Travis::Model::EncryptedColumn.new(disable: true)

def type
if self.token =~ /svg-/
:svg
else
:default
end
end

protected

def generate_token
Expand Down
19 changes: 16 additions & 3 deletions lib/travis/model/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class User < Travis::Model

before_create :set_as_recent
after_create :create_a_token
after_create :create_svg_token
before_save :track_previous_changes

serialize :github_scopes
Expand All @@ -40,7 +41,11 @@ def with_email(email_address)
end

def token
tokens.first.try(:token)
tokens.find { |t| t.try(:type) == :default}.try(:token)
end

def svg_token
tokens.find { |t| t.try(:type) == :svg}.try(:token)
end

def to_json
Expand Down Expand Up @@ -162,8 +167,16 @@ def inspect
github_oauth_token ? super.gsub(github_oauth_token, '[REDACTED]') : super
end

def create_a_token
self.tokens.create!
def create_a_token(type = :default)
token = self.tokens.create!
if type == :svg
token.token = "svg-#{token.token}"
token.save!
end
end

def create_svg_token
create_a_token(:svg)
end

protected
Expand Down