Class OAuth1

All Implemented Interfaces:
AutoCloseable

public class OAuth1 extends ChilkatObject
<section class="oauth1-summary">

Generate OAuth 1.0 signatures, authorization headers, and signed request URLs.

Chilkat.OAuth1 builds the signed OAuth 1.0 authentication data needed for APIs that still require OAuth 1.0 request signing. It can generate the signature base string, HMAC key, signature, URL-encoded signature, normalized signed parameter string, Authorization header, and generated URL containing OAuth query parameters. It supports consumer credentials, access tokens, nonce and timestamp generation, extra signed parameters, optional realm handling, HMAC-SHA1, HMAC-SHA256, RSA-SHA1, and RSA-SHA256 signing.

OAuth 1.0 signing

Generate the OAuth 1.0 signature and related authentication values from the HTTP method, request URL, credentials, token, nonce, and timestamp.

Authorization header

Produce the value to send in the HTTP Authorization header when OAuth parameters should be supplied as a header.

Signed URL output

Generate a URL that includes the OAuth 1.0 query parameters when the API expects authentication data in the request URL.

Extra signed parameters

Add or remove request-specific name/value parameters that must be included in the normalized parameter string and signature calculation.

HMAC or RSA signatures

Sign with HMAC-SHA1, HMAC-SHA256, RSA-SHA1, or RSA-SHA256, with RSA private keys supplied by SetRsaKey.

Debuggable signing details

Inspect the exact BaseString, HmacKey, QueryString, Signature, and EncodedSignature used to build the OAuth request.

Common pattern: Set OauthMethod and OauthUrl, provide the consumer key and secret, set any token and token secret, generate or assign Nonce and Timestamp, add request parameters with AddParam, then call Generate. Use AuthorizationHeader when sending OAuth data in an HTTP header, or GeneratedUrl when the signed OAuth parameters should be placed in the URL.

URL guidance: Set OauthUrl to the base request URL without query parameters. Add query parameters separately with AddParam so they are included correctly in the OAuth 1.0 signature calculation.

</section>

Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when done; an unclosed instance is freed when it becomes unreachable.

See Also:
Online reference
  • Constructor Details

    • OAuth1

      public OAuth1()
      Creates a new OAuth1 object.
  • Method Details

    • getAuthorizationHeader

      public String getAuthorizationHeader()
      The authorization header. This is what would be included in the Authorization HTTP request header if it is going to be used as the means for providing the OAuth1 authorization information.
      Returns:
      the value of the AuthorizationHeader property (read-only)
    • getBaseString

      public String getBaseString()
      This is the exact string that was signed. For example, if the signature method is HMAC-SHA1, the BaseString is the exact string that passed to the HMAC-SHA1. An application does not set the BaseString property. The BaseString is exposed as a property to allow for debugging and to see the exact string that is signed.
      Returns:
      the value of the BaseString property (read-only)
    • getConsumerKey

      public String getConsumerKey()
      The consumer key.
      Returns:
      the value of the ConsumerKey property
    • setConsumerKey

      public void setConsumerKey​(String value)
      Sets the ConsumerKey property; see getConsumerKey().
      Parameters:
      value - the new value (not null)
    • getConsumerSecret

      public String getConsumerSecret()
      The consumer secret.
      Returns:
      the value of the ConsumerSecret property
    • setConsumerSecret

      public void setConsumerSecret​(String value)
      Sets the ConsumerSecret property; see getConsumerSecret().
      Parameters:
      value - the new value (not null)
    • getEncodedSignature

      public String getEncodedSignature()
      The URL encoded representation of the Signature property
      Returns:
      the value of the EncodedSignature property (read-only)
    • getGeneratedUrl

      public String getGeneratedUrl()
      The URL that includes the OAuth1 query params.
      Returns:
      the value of the GeneratedUrl property (read-only)
    • getHmacKey

      public String getHmacKey()
      The exact HMAC key used to sign the BaseString. An application does not directly set the HmacKey. The HmacKey property is read-only and is provided for debugging to see the exact HMAC key used to sign the BaseString. The HMAC key is composed from the consumer secret (if it exists) and the token secret (if it exists).
      Returns:
      the value of the HmacKey property (read-only)
    • getNonce

      public String getNonce()
      The nonce.
      Returns:
      the value of the Nonce property
    • setNonce

      public void setNonce​(String value)
      Sets the Nonce property; see getNonce().
      Parameters:
      value - the new value (not null)
    • getOauthMethod

      public String getOauthMethod()
      The HTTP method, such as GET, POST, PUT, DELETE, or HEAD. Defaults to GET.
      Returns:
      the value of the OauthMethod property
    • setOauthMethod

      public void setOauthMethod​(String value)
      Sets the OauthMethod property; see getOauthMethod().
      Parameters:
      value - the new value (not null)
    • getOauthUrl

      public String getOauthUrl()
      The OAuth URL, such as http://echo.lab.madgex.com/echo.ashx. See http://bettiolo.github.io/oauth-reference-page/ to compare Chilkat results with another tool's results.

      Note: The OAuthUrl should not include query parameters. For example, do not set the OAuthUrl equal to

      https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl?script=165&deploy=1

      Instead, set OAuthUrl equal to

      https://rest.sandbox.netsuite.com/app/site/hosting/restlet.nl

      and then subsequently call AddParam for each query parameter.

      Returns:
      the value of the OauthUrl property
    • setOauthUrl

      public void setOauthUrl​(String value)
      Sets the OauthUrl property; see getOauthUrl().
      Parameters:
      value - the new value (not null)
    • getOauthVersion

      public String getOauthVersion()
      The oauth_version. Defaults to 1.0. May be set to the empty string to exclude.
      Returns:
      the value of the OauthVersion property
    • setOauthVersion

      public void setOauthVersion​(String value)
      Sets the OauthVersion property; see getOauthVersion().
      Parameters:
      value - the new value (not null)
    • getQueryString

      public String getQueryString()
      Contains the normalized set of request parameters that are signed. This is a read-only property made available for debugging purposes.
      Returns:
      the value of the QueryString property (read-only)
    • getRealm

      public String getRealm()
      The realm (optional).
      Returns:
      the value of the Realm property
    • setRealm

      public void setRealm​(String value)
      Sets the Realm property; see getRealm().
      Parameters:
      value - the new value (not null)
    • getSignature

      public String getSignature()
      The generated base64 signature.
      Returns:
      the value of the Signature property (read-only)
    • getSignatureMethod

      public String getSignatureMethod()
      The signature method. Defaults to HMAC-SHA1. Other possible choices are HMAC-SHA256, RSA-SHA1, and RSA-SHA256 (or the equivalent RSA-SHA2).
      Returns:
      the value of the SignatureMethod property
    • setSignatureMethod

      public void setSignatureMethod​(String value)
      Sets the SignatureMethod property; see getSignatureMethod().
      Parameters:
      value - the new value (not null)
    • getTimestamp

      public String getTimestamp()
      The timestamp, such as 1441632569.
      Returns:
      the value of the Timestamp property
    • setTimestamp

      public void setTimestamp​(String value)
      Sets the Timestamp property; see getTimestamp().
      Parameters:
      value - the new value (not null)
    • getToken

      public String getToken()
      The token.
      Returns:
      the value of the Token property
    • setToken

      public void setToken​(String value)
      Sets the Token property; see getToken().
      Parameters:
      value - the new value (not null)
    • getTokenSecret

      public String getTokenSecret()
      The token secret
      Returns:
      the value of the TokenSecret property
    • setTokenSecret

      public void setTokenSecret​(String value)
      Sets the TokenSecret property; see getTokenSecret().
      Parameters:
      value - the new value (not null)
    • getUncommonOptions

      public String getUncommonOptions()
      This is a catch-all property to be used for uncommon needs. This property defaults to the empty string and should typically remain empty.

      Can be set to a list of the following comma separated keywords:

      • INCLUDE_REALM - Introduced in v9.5.0.85. Include the Realm in the signature calculation and outputs.

      Returns:
      the value of the UncommonOptions property
    • setUncommonOptions

      public void setUncommonOptions​(String value)
      Sets the UncommonOptions property; see getUncommonOptions().
      Parameters:
      value - the new value (not null)
    • addParam

      public void addParam​(String name, String value)
      Adds an extra name/value parameter to the OAuth1 signature.
      Parameters:
      name - the name argument (not null)
      value - the value argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • genNonce

      public void genNonce​(int numBytes)
      Generates a random nonce numBytes in length and sets the Nonce property to the hex encoded value.
      Parameters:
      numBytes - the numBytes argument
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • genTimestamp

      public void genTimestamp()
      Sets the Timestamp property to the current date/time.
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • generate

      public void generate()
      Generate the signature based on the property settings. Input properties are OauthVersion, OauthMethod, Url, ConsumerKey, ConsumerSecret, Token, TokenSecret, Nonce, and Timestamp. Properties set by this method include: BaseString, Signature, HmacKey, EncodedSignature, QueryString, GeneratedUrl, andAuthorizationHeader.
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • removeParam

      public void removeParam​(String name)
      Removes a name/value parameter from the OAuth1 signature.
      Parameters:
      name - the name argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • setRsaKey

      public void setRsaKey​(PrivateKey privKey)
      Sets the RSA key to be used when the SignatureMethod is set to RSA-SHA1 or RSA-SHA2.
      Parameters:
      privKey - the privKey argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why