Package ch.k43.tools
Class SOFA
java.lang.Object
ch.k43.tools.SOFA
SOFA - Simple OAuth 2.0 Framework for Authentication
This class implements a small and easy to use framework to support Java applications with OAuth 2.0 for machine-to-machine (M2M) authentication.
Use this class only in trusted environments, since the calling application need to pass confidential data (e.g. client secret and/or user password) for authentication.
All confidential data used in this class is stored in temporary, volatile memory (RAM) protected by AES-256 encryption.
The Java Logger framework is supported by writing log entries with Level.FINE and Level.WARNING. Logging is enabled by adding a configuration file SOFA-Logging.properties which is read from the current directory
- Author:
- Andy Brunner andy.brunner@k43.ch
-
Constructor Summary
ConstructorDescriptionCreates a SOFA object and set the authorization server and authorization scope.Creates a SOFA object and set the authorization server and authorization scope. -
Method Summary
Modifier and TypeMethodDescriptionboolean
authenticateWithClientCredentials
(String parmClientId, String parmClientSecret) Calls the authorization server with the client credentials grant type to get an OAuth 2.0 access tokenboolean
authenticateWithPassword
(String parmClientId, String parmClientSecret, String parmUsername, String parmUserPassword) Calls the authorization server with the password grant type to get an OAuth 2.0 access tokenGet or refresh OAuth access tokenGet last error messageGet ready-to-use HTTP authorization header, e.g.int
Get response code of last HTTP transactionGet response data of last HTTP transactionGet response headers of last HTTP transactionlong
Get response time of last HTTP transactionvoid
setHttpTimeoutSec
(int parmTimeOut) Set timeout value for HTTP transactions.
-
Constructor Details
-
SOFA
Creates a SOFA object and set the authorization server and authorization scope. The status of the Java Logging framework can be set to enabled or disabled.- Parameters:
parmAuthorizationServer
- is the OAuth authorization serverparmAuthorizationScope
- is the OAuth authorization scopeparmUseLogging
- specifies whether the Java Logger framework should be used or not
-
SOFA
Creates a SOFA object and set the authorization server and authorization scope.- Parameters:
parmAuthorizationServer
- is the OAuth authorization serverparmAuthorizationScope
- is the OAuth authorization scope
-
-
Method Details
-
authenticateWithPassword
public boolean authenticateWithPassword(String parmClientId, String parmClientSecret, String parmUsername, String parmUserPassword) Calls the authorization server with the password grant type to get an OAuth 2.0 access token- Parameters:
parmClientId
- the client ID (Application ID) created during application registration on the authorization serverparmClientSecret
- the client secret generated during during application registration on the authorization serverparmUsername
- the user nameparmUserPassword
- the user password in clear text- Returns:
- boolean true if successful, false otherwise
- See Also:
-
authenticateWithClientCredentials
Calls the authorization server with the client credentials grant type to get an OAuth 2.0 access token- Parameters:
parmClientId
- the client ID (Application ID) created during application registration on the authorization serverparmClientSecret
- the client secret generated during during application registration on the authorization server- Returns:
- boolean true if successful, false otherwise
- See Also:
-
setHttpTimeoutSec
public void setHttpTimeoutSec(int parmTimeOut) Set timeout value for HTTP transactions. The initial timeout is 10 seconds.- Parameters:
parmTimeOut
- is the timeout value in seconds for send and receive operations
-
getHttpResponseCode
public int getHttpResponseCode()Get response code of last HTTP transaction- Returns:
- int with the last HTTP response code
-
getHttpResponseHeaders
Get response headers of last HTTP transaction- Returns:
- Properties with all HTTP response headers or NULL
-
getHttpResponseData
Get response data of last HTTP transaction- Returns:
- String with HTTP response data or NULL
-
getHttpResponseTimeMs
public long getHttpResponseTimeMs()Get response time of last HTTP transaction- Returns:
- long with the response time in milliseconds
-
getAccessToken
Get or refresh OAuth access token- Returns:
- String with the last fetched OAuth access code
-
getHttpAuthorizationHeader
Get ready-to-use HTTP authorization header, e.g.Authorization: Bearer ey...jw
- Returns:
- String with a ready to use HTTP authorization header
-
getErrorMessage
Get last error message- Returns:
- String with the last error message
-