Skip to content

Commit f53ce62

Browse files
committed
[ADD] service account authentication from input object
1 parent 6816b3c commit f53ce62

File tree

10 files changed

+9708
-18
lines changed

10 files changed

+9708
-18
lines changed

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
MIT License
3+
4+
Copyright (c) 2021 Pavel Fedyukovich
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# googleads-node-lib

lib/auth/googleSACredential.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { JSONClient } from 'google-auth-library/build/src/auth/googleauth';
2+
import { GoogleAuth, JWTInput, RefreshOptions } from 'google-auth-library';
3+
import { SCOPES } from '@common/constants';
4+
5+
type ScopeOptions = keyof typeof SCOPES;
6+
7+
export class googleSACredential {
8+
private _json: string | JWTInput;
9+
private _scopes: ScopeOptions[];
10+
private _options: RefreshOptions | undefined;
11+
protected auth: JSONClient;
12+
13+
constructor(
14+
json: string | JWTInput,
15+
scopes: ScopeOptions[],
16+
options?: RefreshOptions,
17+
) {
18+
if (!json) {
19+
throw new Error("'json' must be set when using service account flow.");
20+
}
21+
22+
if (!scopes)
23+
throw new Error("'scopes' must be set when using service account flow.");
24+
25+
this._json = json;
26+
this._scopes = scopes;
27+
this._options = options;
28+
29+
this.build();
30+
}
31+
32+
private async build() {
33+
const scopes = SCOPES as Record<string, string>;
34+
35+
let json = this._json as JWTInput;
36+
37+
if (typeof this._json === 'string') {
38+
json = JSON.parse(this._json);
39+
}
40+
41+
this.auth = new GoogleAuth({
42+
scopes: this._scopes.map((scope) => scopes[scope]),
43+
}).fromJSON(json, this._options);
44+
}
45+
46+
async getToken() {
47+
return await (
48+
await this.auth.getAccessToken()
49+
).token;
50+
}
51+
}

lib/auth/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { googleSACredential } from './googleSACredential';

lib/common/constants/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const SCOPES = {
2+
adwords: 'https://www.googleapis.com/auth/adwords',
3+
ad_manager: 'https://www.googleapis.com/auth/dfp',
4+
};

lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { googleSACredential } from './auth';

0 commit comments

Comments
 (0)