register-server-auth.model.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import * as express from 'express'
  2. import { UserRole } from '@shared/models'
  3. import { MOAuthToken, MUser } from '../models'
  4. export type RegisterServerAuthOptions = RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions
  5. export interface RegisterServerAuthenticatedResult {
  6. username: string
  7. email: string
  8. role?: UserRole
  9. displayName?: string
  10. }
  11. export interface RegisterServerExternalAuthenticatedResult extends RegisterServerAuthenticatedResult {
  12. req: express.Request
  13. res: express.Response
  14. }
  15. interface RegisterServerAuthBase {
  16. // Authentication name (a plugin can register multiple auth strategies)
  17. authName: string
  18. // Called by PeerTube when a user from your plugin logged out
  19. // Returns a redirectUrl sent to the client or nothing
  20. onLogout?(user: MUser, req: express.Request): Promise<string>
  21. // Your plugin can hook PeerTube access/refresh token validity
  22. // So you can control for your plugin the user session lifetime
  23. hookTokenValidity?(options: { token: MOAuthToken, type: 'access' | 'refresh' }): Promise<{ valid: boolean }>
  24. }
  25. export interface RegisterServerAuthPassOptions extends RegisterServerAuthBase {
  26. // Weight of this authentication so PeerTube tries the auth methods in DESC weight order
  27. getWeight(): number
  28. // Used by PeerTube to login a user
  29. // Returns null if the login failed, or { username, email } on success
  30. login(body: {
  31. id: string
  32. password: string
  33. }): Promise<RegisterServerAuthenticatedResult | null>
  34. }
  35. export interface RegisterServerAuthExternalOptions extends RegisterServerAuthBase {
  36. // Will be displayed in a block next to the login form
  37. authDisplayName: () => string
  38. onAuthRequest: (req: express.Request, res: express.Response) => void
  39. }
  40. export interface RegisterServerAuthExternalResult {
  41. userAuthenticated (options: RegisterServerExternalAuthenticatedResult): void
  42. }