UNPKG

2.03 kBPlain TextView Raw
1/*
2 * Copyright © 2019 Atomist, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {
18 configurationValue,
19 HandlerContext,
20 Parameters,
21 ProjectOperationCredentials,
22 RemoteRepoRef,
23} from "@atomist/automation-client";
24import { CredentialsResolver } from "@atomist/sdm";
25
26/**
27 * Resolves to single credentials from the configuration
28 */
29@Parameters()
30export class ConfigurationBasedBasicCredentialsResolver implements CredentialsResolver {
31
32 constructor(private readonly paths: { username: string, password: string } = {
33 username: "sdm.git.user",
34 password: "sdm.git.password",
35 }) { }
36
37 public eventHandlerCredentials(context: HandlerContext): ProjectOperationCredentials {
38 return this.credentialsFromConfiguration();
39 }
40
41 public commandHandlerCredentials(context: HandlerContext, id: RemoteRepoRef): ProjectOperationCredentials {
42 return this.credentialsFromConfiguration();
43 }
44
45 private credentialsFromConfiguration(): ProjectOperationCredentials & { username: string, password: string} {
46 const creds = {
47 username: configurationValue<string>(this.paths.username),
48 password: configurationValue<string>(this.paths.password),
49 };
50 if (!creds.username) {
51 throw new Error(`Git username missing at '${this.paths.username}'`);
52 }
53 if (!creds.password) {
54 throw new Error(`Git password missing at '${this.paths.password}'`);
55 }
56 return creds;
57 }
58}