Added avatar settings and retrieve helper method (#19726)

This commit is contained in:
Mauro Mugnaini 2020-08-12 20:12:21 +02:00
parent 1536523d53
commit 7e035d24c2
7 changed files with 37 additions and 51 deletions

View File

@ -1,7 +1,9 @@
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
# Changelog for "oidc-library"
## [v1.1.0-SNAPSHOT]
- Added avatar configuration and retrieve helper method (#19726)
## [v1.0.0-SNAPSHOT]
- First release (#19225) (#19226) (#19227)
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

View File

@ -11,7 +11,7 @@
</parent>
<groupId>org.gcube.common</groupId>
<artifactId>oidc-library</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>

View File

@ -1,25 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID></ID>
<Type>Portlet</Type>
<Profile>
<Description>${project.description}</Description>
<Class>PortletUser</Class>
<Name>${project.artifactId}</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Name>${project.artifactId}</Name>
<Description>${project.description}</Description>
<MavenCoordinates>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</MavenCoordinates>
<Files>
<File>${project.build.finalName}.${project.packaging}</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

View File

@ -1,18 +0,0 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>descriptor.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>servicearchive</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

View File

@ -65,7 +65,7 @@ public class JWTToken implements Serializable {
return (String) token.get("refresh_token");
}
public String getAsBearer() {
public String getAccessTokenAsBearer() {
return "Bearer " + getAccessTokenString();
}

View File

@ -18,4 +18,6 @@ public interface OpenIdConnectConfiguration {
URL getAuthorizationURL();
URL getAvatarURL();
}

View File

@ -1,7 +1,9 @@
package org.gcube.oidc.rest;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
@ -93,10 +95,10 @@ public class OpenIdConnectRESTHelper {
return JWTToken.fromString(sb.toString());
}
protected static HttpURLConnection performURLEncodedPOSTSendData(URL tokenURL, Map<String, List<String>> params,
protected static HttpURLConnection performURLEncodedPOSTSendData(URL url, Map<String, List<String>> params,
String authorization) throws IOException, ProtocolException, UnsupportedEncodingException {
HttpURLConnection con = (HttpURLConnection) tokenURL.openConnection();
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
@ -193,7 +195,7 @@ public class OpenIdConnectRESTHelper {
params.put("client_id", Arrays.asList(URLEncoder.encode(clientId, "UTF-8")));
params.put("refresh_token", Arrays.asList(token.getRefreshTokenString()));
logger.info("Performing logut from OIDC server with URL: " + logoutUrl);
HttpURLConnection httpURLConnection = performURLEncodedPOSTSendData(logoutUrl, params, token.getAsBearer());
HttpURLConnection httpURLConnection = performURLEncodedPOSTSendData(logoutUrl, params, token.getAccessTokenAsBearer());
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 204) {
logger.info("Logout performed correctly");
@ -203,4 +205,27 @@ public class OpenIdConnectRESTHelper {
}
return false;
}
public static byte[] getUserAvatar(URL avatarURL, JWTToken token) throws Exception {
HttpURLConnection conn = (HttpURLConnection) avatarURL.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(false);
conn.setDoInput(true);
conn.setRequestProperty("Accept", "image/png, image/jpeg, image/gif");
if (token != null) {
String authorization = token.getAccessTokenAsBearer();
logger.debug("Adding authorization header as: {}", authorization);
conn.setRequestProperty("Authorization", authorization);
}
InputStream is = conn.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
}
}