import {
	Jwt,
	RefreshableJwt,
	ServiceAccessData as ServiceCredentialData,
} from '../auth/types'
import { AuthService } from '../auth'
import { ICredential, CredentialType } from './credential'

export class ServiceCredentials implements ICredential {
	readonly type: CredentialType.Service
	private jwt?: Jwt

	constructor(private credentials: ServiceCredentialData) {}

	getToken(): Jwt | RefreshableJwt {
		return this.jwt
	}

	async authorize(authService: AuthService): Promise<Jwt> {
		this.jwt = await authService.serviceLogin(this.credentials)
		return this.jwt
	}

	refreshToken(authService: AuthService) {
		return this.authorize(authService)
	}
}
