7676afa6c7bf856bc2750bb09f74d1ad5db785f7
[so.git] / cloudify-client / src / main / java / org / onap / so / cloudify / base / client / CloudifyClientTokenProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.cloudify.base.client;
22
23 import java.util.Date;
24
25 import org.apache.commons.lang.time.DateUtils;
26
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;
30
31 /**
32  * Cloudify Token Provider that uses the Cloudify client API itself to obtain a token
33  * 
34  * @author JC1348
35  *
36  */
37 public class CloudifyClientTokenProvider implements CloudifyTokenProvider {
38
39         String user;
40         String password;
41         String token;
42         Date expiration;
43         Cloudify cloudify = null;
44
45         public CloudifyClientTokenProvider(String cloudifyEndpoint, String user, String password) {
46                 this.user = user;
47                 this.password = password;
48                 
49                 cloudify = new Cloudify (cloudifyEndpoint);
50         }
51
52         @Override
53         public String getToken() {
54                 Date now = new Date();
55                 if (token != null && expiration != null && expiration.after(now)) {
56                         return token;
57                 }
58
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();
63                 
64                 token = newToken.getValue();
65                 
66                 if (expiration == null) {
67                         expiration = new Date();
68                 }
69                 // TODO:  Make this property driven (or see if it comes back somehow in response)
70                 expiration = DateUtils.addMinutes(expiration, 10);
71                 
72                 return token;
73         }
74
75         @Override
76         /**
77          * This doesn't actually expire the token in Cloudify.  It just prevents this token provider
78          * from using it.
79          */
80         public void expireToken() {
81                 expiration = null;
82                 token = null;
83         }
84
85 }