or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-5/

OAuth Request Signer

A flexible OAuth 1.0 request signing utility that supports multiple signature methods based on configuration.

Requirements

Configuration-driven Signature Selection

Implement a request signing function that dynamically selects the appropriate OAuth 1.0 signature method based on a configuration object. The function should support all four standard OAuth signature methods and generate valid signatures for HTTP requests.

Input Parameters

The signing function should accept:

  • config: An object containing:
    • method: The signature method name (string: 'HMAC-SHA1', 'HMAC-SHA256', 'RSA-SHA1', or 'PLAINTEXT')
    • httpMethod: The HTTP method (e.g., 'GET', 'POST')
    • url: The request URL
    • params: Request parameters (object with key-value pairs)
    • consumerSecret: The OAuth consumer secret
    • tokenSecret: The OAuth token secret (optional, defaults to empty string if not provided)
    • privateKey: PEM-formatted RSA private key (required only when method is 'RSA-SHA1')

Output

The function should return the OAuth signature string.

Error Handling

  • The function throws an error if an unsupported signature method is specified @test

Test Cases

  • Given a configuration with method 'HMAC-SHA1', the function generates a valid HMAC-SHA1 signature @test
  • Given a configuration with method 'HMAC-SHA256', the function generates a valid HMAC-SHA256 signature @test
  • Given a configuration with method 'RSA-SHA1' and a valid private key, the function generates a valid RSA-SHA1 signature @test

Implementation

@generates

API

/**
 * Signs an OAuth request using the specified signature method from configuration.
 *
 * @param {Object} config - Configuration object for signing
 * @param {string} config.method - Signature method: 'HMAC-SHA1', 'HMAC-SHA256', 'RSA-SHA1', or 'PLAINTEXT'
 * @param {string} config.httpMethod - HTTP method (e.g., 'GET', 'POST')
 * @param {string} config.url - Request URL
 * @param {Object} config.params - Request parameters
 * @param {string} config.consumerSecret - OAuth consumer secret
 * @param {string} [config.tokenSecret=''] - OAuth token secret (optional, defaults to empty string)
 * @param {string} [config.privateKey] - PEM-formatted RSA private key (required only for RSA-SHA1)
 * @returns {string} The OAuth signature
 * @throws {Error} Throws an error if the signature method is unsupported
 */
function signRequest(config) {
  // IMPLEMENTATION HERE
}

module.exports = {
  signRequest,
};

Dependencies { .dependencies }

oauth-sign { .dependency }

Provides OAuth 1.0 signature generation support.