Skip to content

Commit e44ab35

Browse files
committed
changes
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 28d96f2 commit e44ab35

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.logsws;
19+
20+
import java.security.GeneralSecurityException;
21+
import java.util.Base64;
22+
23+
import javax.crypto.Cipher;
24+
import javax.crypto.spec.SecretKeySpec;
25+
26+
import com.cloud.serializer.GsonHelper;
27+
28+
public class LogsWebSessionTokenCryptoUtil {
29+
private static final String ALGORITHM = "AES";
30+
public static final String TRANSFORMATION = "AES";
31+
32+
public static String encrypt(LogsWebSessionTokenPayload payload, String key) throws GeneralSecurityException {
33+
String json = GsonHelper.getGson().toJson(payload);
34+
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
35+
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
36+
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
37+
byte[] encrypted = cipher.doFinal(json.getBytes());
38+
return Base64.getEncoder().encodeToString(encrypted);
39+
}
40+
41+
public static LogsWebSessionTokenPayload decrypt(String token, String key) throws GeneralSecurityException {
42+
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
43+
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
44+
cipher.init(Cipher.DECRYPT_MODE, secretKey);
45+
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(token));
46+
String json = new String(decrypted);
47+
return GsonHelper.getGson().fromJson(json, LogsWebSessionTokenPayload.class);
48+
}
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.logsws;
19+
20+
public class LogsWebSessionTokenPayload {
21+
private final String sessionUuid;
22+
private final String creatorAddress;
23+
24+
public LogsWebSessionTokenPayload(String sessionUuid, String creatorAddress) {
25+
this.sessionUuid = sessionUuid;
26+
this.creatorAddress = creatorAddress;
27+
}
28+
29+
public String getSessionUuid() {
30+
return sessionUuid;
31+
}
32+
33+
public String getCreatorAddress() {
34+
return creatorAddress;
35+
}
36+
}

0 commit comments

Comments
 (0)