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;
25 import org.apache.commons.lang.time.DateUtils;
27 import org.onap.so.cloudify.v3.client.Cloudify;
28 import org.onap.so.cloudify.v3.client.TokensResource.GetToken;
29 import org.onap.so.cloudify.v3.model.Token;
32 * Cloudify Token Provider that uses the Cloudify client API itself to obtain a token
37 public class CloudifyClientTokenProvider implements CloudifyTokenProvider {
43 Cloudify cloudify = null;
45 public CloudifyClientTokenProvider(String cloudifyEndpoint, String user, String password) {
47 this.password = password;
49 cloudify = new Cloudify (cloudifyEndpoint);
53 public String getToken() {
54 Date now = new Date();
55 if (token != null && expiration != null && expiration.after(now)) {
59 // Create a "Get Token" request. Force basic authentication to acquire the token itself.
60 GetToken tokenRequest = cloudify.tokens().token();
61 tokenRequest.setBasicAuthentication(user, password);
62 Token newToken = tokenRequest.execute();
64 token = newToken.getValue();
66 if (expiration == null) {
67 expiration = new Date();
69 // TODO: Make this property driven (or see if it comes back somehow in response)
70 expiration = DateUtils.addMinutes(expiration, 10);
77 * This doesn't actually expire the token in Cloudify. It just prevents this token provider
80 public void expireToken() {