forked from oracle/oci-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMfaTotpExample.java
More file actions
115 lines (99 loc) · 5.13 KB
/
MfaTotpExample.java
File metadata and controls
115 lines (99 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.identity.Identity;
import com.oracle.bmc.identity.IdentityClient;
import com.oracle.bmc.identity.model.MfaTotpDevice;
import com.oracle.bmc.identity.model.MfaTotpDeviceSummary;
import com.oracle.bmc.identity.requests.CreateMfaTotpDeviceRequest;
import com.oracle.bmc.identity.requests.GetMfaTotpDeviceRequest;
import com.oracle.bmc.identity.requests.DeleteMfaTotpDeviceRequest;
import com.oracle.bmc.identity.requests.ListMfaTotpDevicesRequest;
import com.oracle.bmc.identity.responses.ListMfaTotpDevicesResponse;
/*
* Example Class to show the usage of MFA TOTP API.
* This sample will do the following things:
* 1) Create MFA totp device
* 2) Get the MFA device
* 3) List the MFA Devices
* 4) Delete the MFA Device
* Note MFA device must be activated manually via the console
*/
public class MfaTotpExample {
public static void main(String[] args) throws Exception {
// TODO: Fill in this value
String configurationFilePath = "~/.oci/config";
String profile = "DEFAULT";
// Configuring the AuthenticationDetailsProvider. It's assuming there is a default OCI config file
// "~/.oci/config", and a profile in that config with the name "DEFAULT". Make changes to the following
// line if needed and use ConfigFileReader.parse(CONFIG_LOCATION, CONFIG_PROFILE);
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final AuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
final String tenantId = provider.getTenantId();
final String userId = provider.getUserId();
Identity identityClient = new IdentityClient(provider);
//create MFA totp device
MfaTotpDevice device = createMfaTotpDevice(identityClient, userId);
//get MFA totp device
MfaTotpDeviceSummary summary =
getMfaTotpDeviceSummary(identityClient, userId, device.getId());
// List all MFA Totp Devices for specified user
System.out.println("ListMfaTotpDevices for user: " + userId + "\n");
ListMfaTotpDevicesResponse response =
identityClient.listMfaTotpDevices(
ListMfaTotpDevicesRequest.builder().userId(userId).limit(1).build());
System.out.println("ListMfaTotpDevices Response for : " + userId + "\n");
for (MfaTotpDeviceSummary mfaTotpDevice : response.getItems()) {
System.out.println(mfaTotpDevice);
}
//delete MFA totp device
deleteMfaTotpDevice(identityClient, userId, device.getId());
System.out.println("Mfa totp device deleted");
}
public static MfaTotpDevice createMfaTotpDevice(Identity client, String userId) {
CreateMfaTotpDeviceRequest req =
CreateMfaTotpDeviceRequest.builder().userId(userId).build();
try {
MfaTotpDevice device = client.createMfaTotpDevice(req).getMfaTotpDevice();
if (device == null) {
throw new RuntimeException("Mfa TOTP device creation failed");
}
System.out.println("Mfa totp " + device + " created successfully");
return device;
} catch (Exception ex) {
throw new RuntimeException(
"MFA Totp device creation fails with error:" + ex.getMessage());
}
}
public static MfaTotpDeviceSummary getMfaTotpDeviceSummary(
Identity client, String userId, String deviceId) {
GetMfaTotpDeviceRequest req =
GetMfaTotpDeviceRequest.builder().userId(userId).mfaTotpDeviceId(deviceId).build();
try {
MfaTotpDeviceSummary summary = client.getMfaTotpDevice(req).getMfaTotpDeviceSummary();
System.out.println("MfaTotpDeviceId" + summary.getId() + " retrieved successfully");
return summary;
} catch (Exception ex) {
throw new RuntimeException("getMfaTotpDevice fails with error:" + ex.getMessage());
}
}
public static boolean deleteMfaTotpDevice(Identity client, String userId, String deviceId) {
DeleteMfaTotpDeviceRequest deleteMfaTotpDeviceRequest =
DeleteMfaTotpDeviceRequest.builder()
.userId(userId)
.mfaTotpDeviceId(deviceId)
.build();
try {
client.deleteMfaTotpDevice(deleteMfaTotpDeviceRequest);
System.out.println("Mfa Totp device " + deviceId + " deleted successfully");
return Boolean.TRUE;
} catch (Exception ex) {
throw new RuntimeException("Mfa totp delete fails with error:" + ex.getMessage());
}
}
}