/** * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * * @file src/auth.ts * @author leeight */ import type { HttpHeaders, HttpMethod } from './types/common'; /** 查询参数接口 */ interface QueryParams { [key: string]: string | number | boolean | null | undefined; } /** 头部规范化结果接口 */ interface HeadersCanonicalizationResult { 0: string; 1: string[]; } /** * 百度云认证类 * * @example * ```typescript * const auth = new Auth('your-access-key', 'your-secret-key'); * const authorization = auth.generateAuthorization('GET', '/path', {}, {}, Date.now() / 1000); * ``` */ export declare class Auth { /** Access Key */ readonly ak: string; /** Secret Key */ readonly sk: string; /** * 构造函数 * @param ak Access Key * @param sk Secret Key */ constructor(ak: string, sk: string); /** * 生成授权签名 * 基于 http://gollum.baidu.com/AuthenticationMechanism * * @param method HTTP 请求方法 * @param resource 请求路径 * @param params 查询字符串参数 * @param headers HTTP 请求头 * @param timestamp 当前时间戳(秒) * @param expirationInSeconds 签名有效期(秒) * @param headersToSign 用于计算签名的请求头列表 * @returns 签名字符串 */ generateAuthorization(method: HttpMethod, resource: string, params?: QueryParams, headers?: HttpHeaders, timestamp?: number, expirationInSeconds?: number, headersToSign?: string[]): string; /** * URI 规范化 * @param uri 原始 URI * @returns 规范化后的 URI */ uriCanonicalization(uri: string): string; /** * 查询字符串规范化 * @see http://gollum.baidu.com/AuthenticationMechanism#生成CanonicalQueryString * @param params 查询字符串参数 * @returns 规范化的查询字符串 */ queryStringCanonicalization(params: QueryParams): string; /** * HTTP 请求头规范化 * @see http://gollum.baidu.com/AuthenticationMechanism#生成CanonicalHeaders * @param headers HTTP 请求头 * @param headersToSign 用于计算签名的请求头列表 * @returns 规范化的头部和签名头部列表 */ headersCanonicalization(headers: HttpHeaders, headersToSign?: string[]): HeadersCanonicalizationResult; /** * 计算 HMAC-SHA256 哈希 * @param data 要哈希的数据 * @param key 密钥 * @returns 十六进制哈希值 */ hash(data: string, key: string): string; /** * 获取 ISO8601 格式的时间戳 * @param timestamp 时间戳(秒),如果不提供则使用当前时间 * @returns ISO8601 格式的时间字符串 */ getTimestamp(timestamp?: number): string; /** * 字符串规范化(URL 编码) * @param string 要编码的字符串 * @param encodingSlash 是否编码斜杠 * @returns 编码后的字符串 */ normalize(string: string | null, encodingSlash?: boolean): string; /** * 生成规范化 URI * @param resourceUrl 资源 URL * @returns 规范化的 URI */ generateCanonicalUri(resourceUrl: string): string; } export default Auth; //# sourceMappingURL=auth.d.ts.map