UNPKG

@adonisjs/ally

Version:

Social authentication provider for AdonisJS

149 lines (147 loc) 4.31 kB
import { Oauth1Driver } from "../../chunk-STBND7WI.js"; import "../../chunk-NK6X76EQ.js"; import "../../chunk-TSIMPJ6I.js"; // src/drivers/twitter.ts var TwitterDriver = class extends Oauth1Driver { /** * @param ctx - The HTTP context * @param config - Configuration for the Twitter driver */ constructor(ctx, config) { super(ctx, config); this.ctx = ctx; this.config = config; this.loadState(); } ctx; config; /** * Twitter request-token endpoint URL. */ requestTokenUrl = "https://api.twitter.com/oauth/request_token"; /** * Twitter authorization endpoint URL. */ authorizeUrl = "https://api.twitter.com/oauth/authenticate"; /** * Twitter access-token endpoint URL. */ accessTokenUrl = "https://api.twitter.com/oauth/access_token"; /** * Twitter profile endpoint URL. */ userInfoUrl = "https://api.twitter.com/1.1/account/verify_credentials.json"; /** * The query string param name for the error. */ errorParamName = "error"; /** * The query string param name for the "oauth_verifier". Used * for both the post redirect value access and during the * time of generating the access token */ oauthTokenVerifierName = "oauth_verifier"; /** * Cookie name for storing the oauth_token. The cookie * name for storing oauth_token_secret is derived * from this property */ oauthTokenCookieName = "twitter_oauth_token"; /** * Param name for defined the "oauth_token" pre redirect * and also used post redirect for reading the "oauth_token" * value */ oauthTokenParamName = "oauth_token"; /** * Twitter doesn't support scopes */ scopeParamName = ""; /** * Scope separator placeholder maintained for OAuth1 compatibility. */ scopesSeparator = " "; /** * Fetches the authenticated user's profile information from the Twitter API. * * @param token - The OAuth token * @param secret - The OAuth token secret * @param callback - Optional callback to customize the API request * * @see https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/manage-account-settings/api-reference/get-account-verify_credentials */ async getUserInfo(token, secret, callback) { const requestToken = { token, secret }; const userInfoUrl = this.config.userInfoUrl || this.userInfoUrl; const user = await this.makeSignedRequest(userInfoUrl, "get", requestToken, (request) => { request.param("include_email", true); request["parseAs"]("json"); if (typeof callback === "function") { callback(request); } }); return { id: user.id_str, nickName: user.screen_name, name: user.name || user.screen_name, email: user.email, emailVerificationState: "unsupported", avatarUrl: user.profile_image_url_https.replace("_normal.jpg", "_400x400.jpg"), original: user }; } /** * Get the authenticated user's profile information using * the OAuth verifier from the callback request. * * @param callback - Optional callback to customize the API request * * @example * ```ts * const user = await ally.use('twitter').user() * console.log(user.name, user.email) * ``` */ async user(callback) { const token = await this.accessToken(); const userInfo = await this.getUserInfo(token.token, token.secret, callback); return { ...userInfo, token }; } /** * Get the user's profile information using an existing OAuth token * and token secret. * * @param token - The OAuth token * @param secret - The OAuth token secret * @param callback - Optional callback to customize the API request * * @example * ```ts * const user = await ally.use('twitter').userFromTokenAndSecret(token, secret) * ``` */ async userFromTokenAndSecret(token, secret, callback) { const userInfo = await this.getUserInfo(token, secret, callback); return { ...userInfo, token: { token, secret } }; } /** * Check if the error from the callback indicates that the user * denied authorization. * * @returns `true` when the request contains the denial marker. */ accessDenied() { return this.ctx.request.input("denied"); } }; export { TwitterDriver };