Class Credentials

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public class Credentials extends GObject

The GCredentials type is a reference-counted wrapper for native credentials.

The information in GCredentials is typically used for identifying, authenticating and authorizing other processes.

Some operating systems supports looking up the credentials of the remote peer of a communication endpoint - see e.g. Socket.getCredentials().

Some operating systems supports securely sending and receiving credentials over a Unix Domain Socket, see UnixCredentialsMessage, UnixConnection.sendCredentials(Cancellable) and UnixConnection.receiveCredentials(Cancellable) for details.

On Linux, the native credential type is a struct ucred - see the unix(7) man page for details. This corresponds to G_CREDENTIALS_TYPE_LINUX_UCRED.

On Apple operating systems (including iOS, tvOS, and macOS), the native credential type is a struct xucred. This corresponds to G_CREDENTIALS_TYPE_APPLE_XUCRED.

On FreeBSD, Debian GNU/kFreeBSD, and GNU/Hurd, the native credential type is a struct cmsgcred. This corresponds to G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED.

On NetBSD, the native credential type is a struct unpcbid. This corresponds to G_CREDENTIALS_TYPE_NETBSD_UNPCBID.

On OpenBSD, the native credential type is a struct sockpeercred. This corresponds to G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED.

On Solaris (including OpenSolaris and its derivatives), the native credential type is a ucred_t. This corresponds to G_CREDENTIALS_TYPE_SOLARIS_UCRED.

Since GLib 2.72, on Windows, the native credentials may contain the PID of a process. This corresponds to G_CREDENTIALS_TYPE_WIN32_PID.

Since:
2.26
  • Constructor Details

    • Credentials

      public Credentials(MemorySegment address)
      Create a Credentials instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Credentials

      public Credentials()
      Create a new Credentials.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Credentials class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Credentials asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • getNative

      public @Nullable MemorySegment getNative(CredentialsType nativeType)

      Gets a pointer to native credentials of type nativeType from credentials.

      It is a programming error (which will cause a warning to be logged) to use this method if there is no GCredentials support for the OS or if nativeType isn't supported by the OS.

      Parameters:
      nativeType - The type of native credentials to get.
      Returns:
      The pointer to native credentials or null if there is no GCredentials support for the OS or if nativeType isn't supported by the OS. Do not free the returned data, it is owned by credentials.
      Since:
      2.26
    • getUnixPid

      public int getUnixPid() throws GErrorException

      Tries to get the UNIX process identifier from credentials. This method is only available on UNIX platforms.

      This operation can fail if GCredentials is not supported on the OS or if the native credentials type does not contain information about the UNIX process ID.

      Returns:
      The UNIX process ID, or -1 if error is set.
      Throws:
      GErrorException - see GError
      Since:
      2.36
    • getUnixUser

      public int getUnixUser() throws GErrorException

      Tries to get the UNIX user identifier from credentials. This method is only available on UNIX platforms.

      This operation can fail if GCredentials is not supported on the OS or if the native credentials type does not contain information about the UNIX user.

      As the signedness of uid_t is not specified by POSIX, it is recommended to check error for failure rather than trying to check the return value, particularly in language bindings.

      Returns:
      The UNIX user identifier or (uid_t) -1 if error is set.
      Throws:
      GErrorException - see GError
      Since:
      2.26
    • isSameUser

      public boolean isSameUser(Credentials otherCredentials) throws GErrorException

      Checks if this Credentials and otherCredentials is the same user.

      This operation can fail if GCredentials is not supported on the the OS.

      Parameters:
      otherCredentials - A GCredentials.
      Returns:
      true if this Credentials and otherCredentials has the same user, false otherwise or if error is set.
      Throws:
      GErrorException - see GError
      Since:
      2.26
    • setNative

      public void setNative(CredentialsType nativeType, MemorySegment native_)

      Copies the native credentials of type nativeType from native_ into credentials.

      It is a programming error (which will cause a warning to be logged) to use this method if there is no GCredentials support for the OS or if nativeType isn't supported by the OS.

      Parameters:
      nativeType - The type of native credentials to set.
      native_ - A pointer to native credentials.
      Since:
      2.26
    • setUnixUser

      public boolean setUnixUser(int uid) throws GErrorException

      Tries to set the UNIX user identifier on credentials. This method is only available on UNIX platforms.

      This operation can fail if GCredentials is not supported on the OS or if the native credentials type does not contain information about the UNIX user. It can also fail if the OS does not allow the use of "spoofed" credentials.

      Parameters:
      uid - The UNIX user identifier to set.
      Returns:
      true if uid was set, false if error is set.
      Throws:
      GErrorException - see GError
      Since:
      2.26
    • toString

      public String toString()
      Creates a human-readable textual representation of this Credentials that can be used in logging and debug messages. The format of the returned string may change in future GLib release.
      Overrides:
      toString in class Object
      Returns:
      A string that should be freed with g_free().
      Since:
      2.26
    • builder

      public static Credentials.Builder<? extends Credentials.Builder> builder()
      A Credentials.Builder object constructs a Credentials with the specified properties. Use the various set...() methods to set properties, and finish construction with Credentials.Builder.build().
      Returns:
      the builder object