2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.cloudify.base.client;
23 import java.util.Date;
24 import org.apache.commons.lang.time.DateUtils;
25 import org.onap.so.cloudify.v3.client.Cloudify;
26 import org.onap.so.cloudify.v3.client.TokensResource.GetToken;
27 import org.onap.so.cloudify.v3.model.Token;
30 * Cloudify Token Provider that uses the Cloudify client API itself to obtain a token
35 public class CloudifyClientTokenProvider implements CloudifyTokenProvider {
41 Cloudify cloudify = null;
43 public CloudifyClientTokenProvider(String cloudifyEndpoint, String user, String password) {
45 this.password = password;
47 cloudify = new Cloudify(cloudifyEndpoint);
51 public String getToken() {
52 Date now = new Date();
53 if (token != null && expiration != null && expiration.after(now)) {
57 // Create a "Get Token" request. Force basic authentication to acquire the token itself.
58 GetToken tokenRequest = cloudify.tokens().token();
59 tokenRequest.setBasicAuthentication(user, password);
60 Token newToken = tokenRequest.execute();
62 token = newToken.getValue();
64 if (expiration == null) {
65 expiration = new Date();
67 // TODO: Make this property driven (or see if it comes back somehow in response)
68 expiration = DateUtils.addMinutes(expiration, 10);
75 * This doesn't actually expire the token in Cloudify. It just prevents this token provider from using it.
77 public void expireToken() {