honojs-middleware/packages/oauth-providers/src/providers/twitch/revokeToken.ts

40 lines
1.2 KiB
TypeScript
Raw Normal View History

feat(oauth-providers): Add Twitch OAuth Provider (#981) * feat(twitch): Add type definitions for Twitch OAuth scopes * feat(twitch): Add additional type definitions for Twitch moderator scopes * feat(twitch): Add IRC and PubSub-specific chat scopes to types * feat(twitch): Add new type definitions for Twitch API responses * feat(twitch): Add new user-related scopes for Twitch API * feat(twitch): Add type definitions and import paths for Twitch provider * feat(twitch): Implement Twitch OAuth handlers and response types for mock api * feat(twitch): Add revokeToken function to handle OAuth token revocation * feat(twitch): Add Twitch OAuth middleware mocks and tests * feat(twitch): Implement Twitch OAuth authentication flow and user data retrieval * feat(twitch): Add custom state handling for Twitch OAuth middleware * docs(twitch): Update README with Twitch OAuth integration details * docs: Update Twitch API reference link for scopes in README * fix(twitch): Remove error handling for error_description in auth flow * refactor(twitch): Update token handling and response types for refresh and revoke * feat(twitch): Add token validation function for Twitch OAuth * feat(twitch): Add token validation handler and update response types * docs: Add token validation section to README for Twitch integration * chore(oauth-providers): changesets summary * fix(twitch): make redirect_uri optional in twitchAuth options * refactor(twitch): clean up commented code and improve test assertions * refactor(twitch): improve type assertions for JSON responses * refactor(twitch): update type assertion for JSON response handling * semver amendment Changed version from patch to minor * docs: update README with token validation instructions for Twitch --------- Co-authored-by: Younis-Ahmed <23105954+jonaahmed@users.noreply.github.com>
2025-03-12 16:55:47 +08:00
import { HTTPException } from 'hono/http-exception'
import { toQueryParams } from '../../utils/objectToQuery'
import type { TwitchRevokingResponse } from './types'
export async function revokeToken(
client_id: string,
token: string
): Promise<boolean> {
const params = toQueryParams({
client_id: client_id,
token,
})
const res = await fetch('https://id.twitch.tv/oauth2/revoke', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: params,
})
// Check HTTP status code first
if (!res.ok) {
// Try to parse error response
try {
const errorResponse = await res.json() as TwitchRevokingResponse
if (errorResponse && typeof errorResponse === 'object' && 'message' in errorResponse) {
throw new HTTPException(400, { message: errorResponse.message })
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
// If parsing fails, throw a generic error with the status
throw new HTTPException(400, { message: `Token revocation failed with status: ${res.status}` })
}
}
// Success case - Twitch returns 200 with empty body on successful revocation
return true
}