diff options
Diffstat (limited to 'packages/taler-util/src/http-client/authentication.ts')
-rw-r--r-- | packages/taler-util/src/http-client/authentication.ts | 95 |
1 files changed, 74 insertions, 21 deletions
diff --git a/packages/taler-util/src/http-client/authentication.ts b/packages/taler-util/src/http-client/authentication.ts index b27a266e9..8897a2fa0 100644 --- a/packages/taler-util/src/http-client/authentication.ts +++ b/packages/taler-util/src/http-client/authentication.ts @@ -14,11 +14,29 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +/** + * Imports. + */ import { HttpStatusCode } from "../http-status-codes.js"; -import { HttpRequestLibrary, createPlatformHttpLib, makeBasicAuthHeader } from "../http.js"; +import { + HttpRequestLibrary, + createPlatformHttpLib, + makeBasicAuthHeader, + readTalerErrorResponse, +} from "../http.js"; import { LibtoolVersion } from "../libtool-version.js"; -import { opEmptySuccess, opKnownFailure, opSuccess, opUnknownFailure } from "../operation.js"; -import { AccessToken, TalerAuthentication, codecForTokenSuccessResponse } from "./types.js"; +import { + opEmptySuccess, + opKnownHttpFailure, + opSuccessFromHttp, + opUnknownFailure, +} from "../operation.js"; +import { + AccessToken, + TalerAuthentication, + codecForTokenSuccessResponse, + codecForTokenSuccessResponseMerchant, +} from "./types.js"; import { makeBearerTokenAuthHeader } from "./utils.js"; export class TalerAuthenticationHttpClient { @@ -28,23 +46,23 @@ export class TalerAuthenticationHttpClient { constructor( readonly baseUrl: string, - readonly username: string, httpClient?: HttpRequestLibrary, ) { this.httpLib = httpClient ?? createPlatformHttpLib(); } isCompatible(version: string): boolean { - const compare = LibtoolVersion.compare(this.PROTOCOL_VERSION, version) - return compare?.compatible ?? false + const compare = LibtoolVersion.compare(this.PROTOCOL_VERSION, version); + return compare?.compatible ?? false; } /** * https://docs.taler.net/core/api-corebank.html#post--accounts-$USERNAME-token - * - * @returns + * + * @returns */ - async createAccessToken( + async createAccessTokenBasic( + username: string, password: string, body: TalerAuthentication.TokenRequest, ) { @@ -52,16 +70,49 @@ export class TalerAuthenticationHttpClient { const resp = await this.httpLib.fetch(url.href, { method: "POST", headers: { - Authorization: makeBasicAuthHeader(this.username, password), + Authorization: makeBasicAuthHeader(username, password), }, - body + body, }); switch (resp.status) { - case HttpStatusCode.Ok: return opSuccess(resp, codecForTokenSuccessResponse()) + case HttpStatusCode.Ok: + return opSuccessFromHttp(resp, codecForTokenSuccessResponse()); //FIXME: missing in docs - case HttpStatusCode.Unauthorized: return opKnownFailure("wrong-credentials", resp) - case HttpStatusCode.NotFound: return opKnownFailure("not-found", resp) - default: return opUnknownFailure(resp, await resp.text()) + case HttpStatusCode.Unauthorized: + return opKnownHttpFailure(resp.status, resp); + case HttpStatusCode.NotFound: + return opKnownHttpFailure(resp.status, resp); + default: + return opUnknownFailure(resp, await readTalerErrorResponse(resp)); + } + } + + /** + * + * @returns + */ + async createAccessTokenBearer( + token: AccessToken, + body: TalerAuthentication.TokenRequest, + ) { + const url = new URL(`token`, this.baseUrl); + const resp = await this.httpLib.fetch(url.href, { + method: "POST", + headers: { + Authorization: makeBearerTokenAuthHeader(token), + }, + body, + }); + switch (resp.status) { + case HttpStatusCode.Ok: + return opSuccessFromHttp(resp, codecForTokenSuccessResponseMerchant()); + //FIXME: missing in docs + case HttpStatusCode.Unauthorized: + return opKnownHttpFailure(resp.status, resp); + case HttpStatusCode.NotFound: + return opKnownHttpFailure(resp.status, resp); + default: + return opUnknownFailure(resp, await readTalerErrorResponse(resp)); } } @@ -71,14 +122,16 @@ export class TalerAuthenticationHttpClient { method: "DELETE", headers: { Authorization: makeBearerTokenAuthHeader(token), - } + }, }); switch (resp.status) { - case HttpStatusCode.Ok: return opEmptySuccess() + case HttpStatusCode.Ok: + return opEmptySuccess(resp); //FIXME: missing in docs - case HttpStatusCode.NotFound: return opKnownFailure("not-found", resp) - default: return opUnknownFailure(resp, await resp.text()) + case HttpStatusCode.NotFound: + return opKnownHttpFailure(resp.status, resp); + default: + return opUnknownFailure(resp, await readTalerErrorResponse(resp)); } } - -}
\ No newline at end of file +} |