This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
132 lines (112 loc) · 3.22 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { createHash, randomFillSync } from "node:crypto";
import { Buffer } from "node:buffer";
const ensure = <T>(v: T | null | undefined, d: T): T => {
return typeof v !== "undefined" && v !== null ? v : d;
};
type SHA1Encoding = "base64" | "latin1" | "hex";
class UsernameToken {
private _created: string;
private _nonce: string;
private _password: string;
private _sha1encoding: SHA1Encoding | undefined;
private _username: string;
constructor({
username,
password,
created,
nonce,
sha1encoding,
}: {
username: string;
password: string;
created?: string;
nonce?: string;
// YOU SHOULD NOT USE THIS OPTION.
// See: https://github.com/bouzuya/node-wsse/issues/1
sha1encoding?: SHA1Encoding;
}) {
this._username = username; // required
this._password = password; // required
this._created = ensure(created, this._newCreated());
this._nonce = ensure(nonce, this._newNonce());
this._sha1encoding = sha1encoding;
}
public getCreated(): string {
return this._created;
}
public getNonce(): string {
return this._nonce;
}
public getNonceBase64(): string {
return this._base64ForString(this._nonce);
}
public getPassword(): string {
return this._password;
}
public getPasswordDigest(): string {
const text = this.getNonce() + this.getCreated() + this.getPassword();
const buf = this._sha1(text);
return typeof this._sha1encoding === "undefined"
? this._base64(buf)
: // workaround for emarsys bad implementation
this._base64ForString(buf.toString(this._sha1encoding));
}
public getUsername(): string {
return this._username;
}
public getWSSEHeader(options?: { nonceBase64?: boolean }): string {
const opts = ensure<{ nonceBase64?: boolean }>(options, {});
// workaround for hatena api
const nonceBase64 = ensure<boolean>(opts.nonceBase64, false);
return (
"UsernameToken " +
[
`Username="${this.getUsername()}"`,
`PasswordDigest="${this.getPasswordDigest()}"`,
`Nonce="${nonceBase64 ? this.getNonceBase64() : this.getNonce()}"`,
`Created="${this.getCreated()}"`,
].join(", ")
);
}
public newToken(): UsernameToken {
// new nonce & new created
return new UsernameToken({
password: this._password,
sha1encoding: this._sha1encoding,
username: this._username,
});
}
public toString(options?: { nonceBase64?: boolean }): string {
return this.getWSSEHeader(options);
}
private _base64(b: Buffer): string {
return b.toString("base64");
}
private _base64ForString(s: string): string {
return this._base64(Buffer.from(s, "utf-8"));
}
private _newCreated(): string {
return new Date().toISOString();
}
private _newNonce(): string {
const buf = Buffer.alloc(10);
randomFillSync(buf);
return buf.toString("hex");
}
private _sha1(s: string): Buffer {
const sha1 = createHash("sha1");
sha1.update(s, "utf-8");
return sha1.digest();
}
}
const usernameToken = ({
username,
password,
}: {
username: string;
password: string;
}): UsernameToken => {
return new UsernameToken({ username, password });
};
export default usernameToken;
export { UsernameToken };