This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
socketio-jwt/types/index.d.ts

87 lines
2.2 KiB
TypeScript
Raw Normal View History

/**
* This module allows to authenticate socket.io connections with JWTs.
* This is especially if you do not want to use cookies in a single page application.
*/
/// <reference types="socket.io" />
declare module 'socketio-jwt' {
/**
* Defines possible errors for the secret-callback.
*/
interface ISocketIOError {
2020-12-27 17:25:44 +01:00
readonly code: string
readonly message: string
}
/**
* Callback gets called, if secret is given dynamically.
*/
interface ISocketCallback {
2020-12-27 17:25:44 +01:00
(err: ISocketIOError, success: string): void
}
interface ISocketIOMiddleware {
2020-12-27 17:25:44 +01:00
(socket: SocketIO.Socket, fn: (err?: any) => void): void
}
interface IOptions {
2020-12-27 17:25:44 +01:00
additional_auth?: (
decoded: object,
onSuccess: () => void,
onError: (err: string | ISocketIOError, code: string) => void
) => void
customDecoded?: (decoded: object) => object
2020-12-27 17:25:44 +01:00
callback?: false | number
secret:
| string
| ((
request: any,
decodedToken: object,
callback: ISocketCallback
) => void)
2020-12-27 17:25:44 +01:00
encodedPropertyName?: string
decodedPropertyName?: string
auth_header_required?: boolean
handshake?: boolean
required?: boolean
timeout?: number
cookie?: string
}
2020-12-27 17:25:44 +01:00
function authorize(
options: IOptions /*, onConnection: Function*/
): ISocketIOMiddleware
interface UnauthorizedError extends Error {
2020-12-27 17:25:44 +01:00
readonly message: string
readonly inner: object
readonly data: { message: string; code: string; type: 'UnauthorizedError' }
}
var UnauthorizedError: {
2020-12-27 17:25:44 +01:00
prototype: UnauthorizedError
new (code: string, error: { message: string }): UnauthorizedError
}
/**
* This is an augmented version of the SocketIO.Server.
* It knows the 'authenticated' event and should be extended in future.
2020-12-27 17:25:44 +01:00
* @see SocketIO.Server
*/
export interface JWTServer extends SocketIO.Server {
/**
* The event gets fired when a new connection is authenticated via JWT.
* @param event The event being fired: 'authenticated'
* @param listener A listener that should take one parameter of type Socket
* @return The default '/' Namespace
*/
2020-12-27 17:25:44 +01:00
on(
event: 'authenticated' | string,
listener: (socket: SocketIO.Socket) => void
): SocketIO.Namespace
}
}