Class: BlueFactory::UserInfo
- Inherits:
-
Object
- Object
- BlueFactory::UserInfo
- Defined in:
- lib/blue_factory/user_info.rb
Overview
An object which provides info about the user making the request, based on the included authorization header (if any). Accessed through RequestContext#user.
Instance Method Summary collapse
-
#initialize(auth_header) ⇒ UserInfo
constructor
A new instance of UserInfo.
-
#raw_did ⇒ String?
Returns the user's (unverified) DID decoded from the JWT payload of the bearer token.
-
#token ⇒ String?
The bearer token extracted from the authorization header.
Constructor Details
#initialize(auth_header) ⇒ UserInfo
Returns a new instance of UserInfo.
18 19 20 |
# File 'lib/blue_factory/user_info.rb', line 18 def initialize(auth_header) @auth = auth_header end |
Instance Method Details
#raw_did ⇒ String?
Returns the user's (unverified) DID decoded from the JWT payload of the bearer token.
Important: this method does not verify the signature of the token, which means the token can be fairly easily forged to impersonate another user, and this method would not detect that. Do not rely on it for use cases where it's important to be certain of the requesting user's identity.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/blue_factory/user_info.rb', line 51 def raw_did return nil if token.nil? parts = token.split('.') raise AuthorizationError.new("Invalid JWT format", "BadJwt") unless parts.length == 3 begin payload = JSON.parse(Base64.decode64(parts[1])) rescue StandardError => e raise AuthorizationError.new("Invalid JWT format", "BadJwt") end if did = payload['iss'] did else raise AuthorizationError.new("Invalid JWT format", "BadJwt") end end |
#token ⇒ String?
The bearer token extracted from the authorization header.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/blue_factory/user_info.rb', line 28 def token @token ||= begin if @auth.nil? || @auth.strip.empty? nil elsif !@auth.start_with?('Bearer ') raise AuthorizationError, "Unsupported authorization method" else @auth.gsub(/^Bearer /, '') end end end |