docs
evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
Implement an OAuth 1.0a client that performs a complete three-legged authentication flow to authorize with a service provider and make authenticated API requests.
Your task is to build an OAuth 1.0a client implementation that handles all three phases of the OAuth authentication flow: obtaining a request token, exchanging it for an access token after user authorization, and making authenticated API calls.
The client should:
All requests must be properly signed according to OAuth 1.0a specifications. Use HMAC-SHA1 as the signature method.
Implement the following in oauth-client.js:
getRequestToken(consumerKey, consumerSecret, callbackUrl, requestTokenUrl)Generates signature data for requesting a temporary token.
Parameters:
consumerKey (string): Your application's consumer keyconsumerSecret (string): Your application's consumer secretcallbackUrl (string): The URL where the provider will redirect after authorizationrequestTokenUrl (string): The provider's request token endpoint URLReturns: An object containing:
signature (string): The OAuth signature for the requestoauthParams (object): All OAuth parameters including signaturebuildAuthorizationUrl(authorizeUrl, requestToken)Constructs the URL where users should be directed to authorize your application.
Parameters:
authorizeUrl (string): The provider's authorization endpoint URLrequestToken (string): The request token obtained from the first stepReturns: The complete authorization URL as a string
getAccessToken(consumerKey, consumerSecret, requestToken, requestTokenSecret, verifier, accessTokenUrl)Generates signature data for exchanging the request token for an access token.
Parameters:
consumerKey (string): Your application's consumer keyconsumerSecret (string): Your application's consumer secretrequestToken (string): The request token from step 1requestTokenSecret (string): The token secret received with the request tokenverifier (string): The verification code received after user authorizationaccessTokenUrl (string): The provider's access token endpoint URLReturns: An object containing:
signature (string): The OAuth signature for the requestoauthParams (object): All OAuth parameters including signature and verifiermakeAuthenticatedRequest(httpMethod, apiUrl, consumerKey, consumerSecret, accessToken, accessTokenSecret, additionalParams)Generates signature data for making authenticated API requests.
Parameters:
httpMethod (string): HTTP method (e.g., 'GET', 'POST')apiUrl (string): The API endpoint URLconsumerKey (string): Your application's consumer keyconsumerSecret (string): Your application's consumer secretaccessToken (string): The access token from step 3accessTokenSecret (string): The token secret received with the access tokenadditionalParams (object): Additional request parameters (query or body parameters)Returns: An object containing:
signature (string): The OAuth signature for the requestoauthParams (object): All OAuth parameters including signatureoauth_consumer_key, oauth_signature_method, oauth_timestamp, oauth_nonce, oauth_version, oauth_signatureoauth_callbackoauth_token and oauth_verifieroauth_tokenoauth_nonce values (can use random strings or UUIDs)oauth_timestampoauth_version to "1.0"Provides OAuth 1.0 signature generation support.
Write tests in oauth-client.test.js to verify your implementation.
Verify that the request token phase generates valid signatures.
Test Code:
const { getRequestToken } = require('./oauth-client');
const result = getRequestToken(
'consumer-key-123',
'consumer-secret-456',
'http://example.com/callback',
'https://api.provider.com/oauth/request_token'
);
// Should return an object with signature and oauthParams
console.assert(typeof result.signature === 'string', 'Signature should be a string');
console.assert(result.signature.length > 0, 'Signature should not be empty');
console.assert(result.oauthParams.oauth_consumer_key === 'consumer-key-123', 'Should include consumer key');
console.assert(result.oauthParams.oauth_callback === 'http://example.com/callback', 'Should include callback URL');
console.assert(result.oauthParams.oauth_signature_method === 'HMAC-SHA1', 'Should use HMAC-SHA1');Verify that the authorization URL is correctly formatted.
Test Code:
const { buildAuthorizationUrl } = require('./oauth-client');
const url = buildAuthorizationUrl(
'https://api.provider.com/oauth/authorize',
'request-token-xyz'
);
// Should return a properly formatted URL
console.assert(url.includes('https://api.provider.com/oauth/authorize'), 'Should contain base URL');
console.assert(url.includes('request-token-xyz'), 'Should contain request token');
console.log('Authorization URL:', url);Verify that the access token exchange generates valid signatures with both consumer and token secrets.
Test Code:
const { getAccessToken } = require('./oauth-client');
const result = getAccessToken(
'consumer-key-123',
'consumer-secret-456',
'request-token-xyz',
'request-secret-789',
'verifier-code-abc',
'https://api.provider.com/oauth/access_token'
);
// Should return an object with signature and oauthParams
console.assert(typeof result.signature === 'string', 'Signature should be a string');
console.assert(result.oauthParams.oauth_token === 'request-token-xyz', 'Should include request token');
console.assert(result.oauthParams.oauth_verifier === 'verifier-code-abc', 'Should include verifier');Verify that authenticated requests include all necessary parameters.
Test Code:
const { makeAuthenticatedRequest } = require('./oauth-client');
const result = makeAuthenticatedRequest(
'POST',
'https://api.provider.com/1.1/statuses/update.json',
'consumer-key-123',
'consumer-secret-456',
'access-token-final',
'access-secret-final',
{ status: 'Hello World' }
);
// Should return an object with signature and oauthParams
console.assert(typeof result.signature === 'string', 'Signature should be a string');
console.assert(result.oauthParams.oauth_token === 'access-token-final', 'Should include access token');
console.assert(result.oauthParams.oauth_signature_method === 'HMAC-SHA1', 'Should use HMAC-SHA1');