Skip to content
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

feat(currentUser): PPT-840 Add JWT check to ensure public access to resources with GET requests #363

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
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
19 changes: 19 additions & 0 deletions src/placeos-rest-api/utilities/current-user.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,25 @@ module PlaceOS::Api
# Parses, and validates JWT if present.
# Throws Error::MissingBearer and JWT::Error.

protected def check_jwt_scope
access = user_token.get_access("public")
block_access = true

if request.method.downcase == "get"
block_access = !access.none? || !access.read?
else
block_access = !access.write?
end

if block_access
Log.warn { {message: "unknown scope #{user_token.scope}", action: "authorize!", host: request.hostname, id: user_token.id} }
raise Error::Unauthorized.new "valid scope required for access"
end
end

def authorize! : Model::UserJWT
unless (token = @user_token).nil?
check_jwt_scope
return token
end

Expand All @@ -25,6 +42,7 @@ module PlaceOS::Api
Log.context.set(api_key_id: api_key.id, api_key_name: api_key.name)
ensure_matching_domain(user_token)
@user_token = user_token
check_jwt_scope
return user_token
rescue e
Log.warn(exception: e) { {message: "bad or unknown X-API-Key", action: "authorize!"} }
Expand All @@ -45,6 +63,7 @@ module PlaceOS::Api
end

ensure_matching_domain(user_token)
check_jwt_scope
user_token
rescue e
# ensure that the user token is nil if this function ever errors.
Expand Down
Loading