forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding MSICredentails type (Azure#250)
* Adding MSICredentails type * Moving adapter as MSICredentails memeber variable
- Loading branch information
1 parent
f7bf707
commit 34666d3
Showing
1 changed file
with
102 additions
and
0 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
...e-client-authentication/src/main/java/com/microsoft/azure/credentials/MSICredentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for | ||
* license information. | ||
*/ | ||
|
||
package com.microsoft.azure.credentials; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.microsoft.azure.AzureEnvironment; | ||
import com.microsoft.azure.management.apigeneration.Beta; | ||
import com.microsoft.azure.serializer.AzureJacksonAdapter; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStreamWriter; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
/** | ||
* Managed Service Identity token based credentials for use with a REST Service Client. | ||
*/ | ||
@Beta | ||
public class MSICredentials extends AzureTokenCredentials { | ||
private final String resource; | ||
private final int msiPort; | ||
private final AzureJacksonAdapter adapter; | ||
/** | ||
* Initializes a new instance of the MSICredentials. | ||
* | ||
* @param environment the Azure environment to use | ||
*/ | ||
public MSICredentials(AzureEnvironment environment) { | ||
this(environment, 50342); | ||
} | ||
|
||
/** | ||
* Initializes a new instance of the MSICredentials. | ||
* | ||
* @param environment the Azure environment to use | ||
* @param msiPort the local port to retrieve token from | ||
*/ | ||
public MSICredentials(AzureEnvironment environment, int msiPort) { | ||
super(environment, null /** retrieving MSI token does not require tenant **/); | ||
this.resource = environment.resourceManagerEndpoint(); | ||
this.msiPort = msiPort; | ||
this.adapter = new AzureJacksonAdapter(); | ||
} | ||
|
||
@Override | ||
public String getToken(String resource) throws IOException { | ||
URL url = new URL(String.format("http://localhost:%d/oauth2/token", this.msiPort)); | ||
String postData = String.format("resource=%s", this.resource); | ||
HttpURLConnection connection = null; | ||
|
||
try { | ||
connection = (HttpURLConnection) url.openConnection(); | ||
|
||
connection.setRequestMethod("POST"); | ||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); | ||
connection.setRequestProperty("Metadata", "true"); | ||
connection.setRequestProperty("Content-Length", Integer.toString(postData.length())); | ||
connection.setDoOutput(true); | ||
|
||
connection.connect(); | ||
|
||
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream()); | ||
wr.write(postData); | ||
wr.flush(); | ||
|
||
InputStream stream = connection.getInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"), 100); | ||
String result = reader.readLine(); | ||
|
||
MSIToken msiToken = adapter.deserialize(result, MSIToken.class); | ||
return msiToken.accessToken; | ||
} finally { | ||
if (connection != null) { | ||
connection.disconnect(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Type representing response from the local MSI token provider. | ||
*/ | ||
private static class MSIToken { | ||
/** | ||
* Token type "Bearer". | ||
*/ | ||
@JsonProperty(value = "token_type") | ||
private String tokenType; | ||
|
||
/** | ||
* Access token. | ||
*/ | ||
@JsonProperty(value = "access_token") | ||
private String accessToken; | ||
} | ||
} |