add qiniu_live.ts sign.ts
This commit is contained in:
parent
5ee32d0abc
commit
52af1b9254
|
|
@ -0,0 +1,56 @@
|
|||
import {base64ToUrlSafe, hmacSha1, urlSafeBase64Encode} from '../sign';
|
||||
|
||||
export default class qiniuLiveInstance {
|
||||
accessKey: string;
|
||||
secretKey: string;
|
||||
host: string; // 请求域名,
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
path: string; // 请求路径 实际的请求路径详见各七牛直播云接口说明的请求包
|
||||
rawQuery?: string;
|
||||
contentType?: string;
|
||||
contentLength?: string;
|
||||
bodyStr?: string;
|
||||
constructor(config: {
|
||||
accessKey: string;
|
||||
secretKey: string;
|
||||
host: string;
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
path: string;
|
||||
rawQuery?: string;
|
||||
contentType?: string;
|
||||
bodyStr?: string;
|
||||
contentLength?: string;
|
||||
}) {
|
||||
const { accessKey, secretKey, host, method, path, rawQuery, contentType, bodyStr, contentLength } = config;
|
||||
this.accessKey = accessKey;
|
||||
this.secretKey = secretKey;
|
||||
this.host = host;
|
||||
this.method = method;
|
||||
this.path = path;
|
||||
this.rawQuery = rawQuery;
|
||||
this.contentType = contentType;
|
||||
this.bodyStr = bodyStr;
|
||||
this.contentLength = contentLength;
|
||||
}
|
||||
|
||||
getToken() {
|
||||
const {method, path, rawQuery, host, contentType, contentLength, bodyStr, accessKey, secretKey} = this;
|
||||
// 1. 添加 Path
|
||||
let data = `${method} ${path}`
|
||||
if (rawQuery) {
|
||||
data += `?${rawQuery}`
|
||||
}
|
||||
data += `\nHost: ${host}`
|
||||
if (contentType) {
|
||||
data += `\nContent-Type: ${contentType}`
|
||||
}
|
||||
data += "\n\n"
|
||||
if(contentLength && bodyStr && contentType && contentType !== "application/octet-stream") {
|
||||
data+=bodyStr;
|
||||
}
|
||||
const sign = hmacSha1(data, secretKey);
|
||||
const encodedSign = urlSafeBase64Encode(sign);
|
||||
const toke = "Qiniu " + accessKey + ":" + encodedSign;
|
||||
return toke;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
import crypto from 'crypto';
|
||||
import { Buffer } from 'buffer';
|
||||
export function base64ToUrlSafe(v: string) {
|
||||
return v.replace(/\//g, '_').replace(/\+/g, '-');
|
||||
}
|
||||
|
||||
export function hmacSha1(encodedFlags: any, secretKey: string) {
|
||||
const hmac = crypto.createHmac('sha1', secretKey);
|
||||
hmac.update(encodedFlags);
|
||||
return hmac.digest('base64');
|
||||
}
|
||||
export function urlSafeBase64Encode(jsonFlags: string) {
|
||||
const encoded = Buffer.from(jsonFlags).toString('base64');
|
||||
return base64ToUrlSafe(encoded);
|
||||
}
|
||||
Loading…
Reference in New Issue