Record Class SecurityIdentity

java.lang.Object
java.lang.Record
com.jsgalactic.axiom.context.SecurityIdentity
Record Components:
principal - the caller's stable name, for example the sub claim of a token; one to 256 characters without control characters
roles - granted roles; copied, each one to 256 characters without control characters
permissions - granted permissions; copied, each one to 256 characters without control characters
attributes - further verified facts about the caller, as text; copied

public record SecurityIdentity(String principal, Set<String> roles, Set<String> permissions, Map<String,String> attributes) extends Record
The authenticated caller of one request: a principal name with the roles and permissions it was granted. Immutable and thread-safe; the sets are unmodifiable copies, so an identity may be handed to application tasks.

An authenticator creates the identity from credentials it verified, and authentication middleware attaches it to the request with Context.identity(SecurityIdentity). Handlers read it with Context.identity(). Role and permission names are compared exactly (case-sensitive). What a role or permission means is the application's decision.

Attributes carry further verified facts about the caller that an authenticator chose to expose (for example a tenant or e-mail claim of a token) as text. They are not grants and no policy reads them; handlers do. At most 64 attributes; a name is 1 to 256 characters and a value 0 to 2,048 characters, neither with control characters.

toString() lists the principal and the number of roles, permissions and attributes only, so a logged identity does not spell out its grants or personal data.

  • Constructor Details

    • SecurityIdentity

      public SecurityIdentity(String principal, Set<String> roles, Set<String> permissions, Map<String,String> attributes)
      Validates and copies the identity.
      Parameters:
      principal - the caller's stable name
      roles - granted roles
      permissions - granted permissions
      attributes - further verified facts, as text
      Throws:
      NullPointerException - if any argument or element is null
      IllegalArgumentException - for an empty, overlong or control-character name, an overlong or control-character attribute value, or more than 64 attributes
    • SecurityIdentity

      public SecurityIdentity(String principal, Set<String> roles, Set<String> permissions)
      Creates an identity with grants and no attributes.
      Parameters:
      principal - the caller's stable name
      roles - granted roles
      permissions - granted permissions
      Throws:
      NullPointerException - if any argument or element is null
      IllegalArgumentException - for an empty, overlong or control-character name
  • Method Details

    • of

      public static SecurityIdentity of(String principal)
      Creates an identity without roles or permissions.
      Parameters:
      principal - the caller's stable name
      Returns:
      identity with no grants
    • hasRole

      public boolean hasRole(String role)
      Reports whether the identity was granted a role.
      Parameters:
      role - exact role name
      Returns:
      true if granted
    • hasPermission

      public boolean hasPermission(String permission)
      Reports whether the identity was granted a permission.
      Parameters:
      permission - exact permission name
      Returns:
      true if granted
    • attribute

      public Optional<String> attribute(String name)
      Reads an attribute.
      Parameters:
      name - exact attribute name
      Returns:
      the value, or empty if the authenticator exposed none with that name
    • withAttributes

      public SecurityIdentity withAttributes(Map<String,String> additional)
      Returns a copy with further attributes; a name already present is replaced.
      Parameters:
      additional - attributes to add
      Returns:
      the new identity
      Throws:
      IllegalArgumentException - as for the constructor
    • toString

      public String toString()
      Describes the identity by principal and grant counts.
      Specified by:
      toString in class Record
      Returns:
      diagnostic description
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • principal

      public String principal()
      Returns the value of the principal record component.
      Returns:
      the value of the principal record component
    • roles

      public Set<String> roles()
      Returns the value of the roles record component.
      Returns:
      the value of the roles record component
    • permissions

      public Set<String> permissions()
      Returns the value of the permissions record component.
      Returns:
      the value of the permissions record component
    • attributes

      public Map<String,String> attributes()
      Returns the value of the attributes record component.
      Returns:
      the value of the attributes record component