Skip to content

Commit

Permalink
Add JWT check to ensure public access to resources with GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
grkek committed Oct 11, 2023
1 parent 8768ba3 commit ecd5950
Showing 1 changed file with 19 additions and 0 deletions.
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?
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

0 comments on commit ecd5950

Please sign in to comment.