Skip to content

Commit 9d59914

Browse files
committed
Set username from standard claim.
1 parent d5ad55c commit 9d59914

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

oauth/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,9 @@
5050
<groupId>com.google.http-client</groupId>
5151
<artifactId>google-http-client-gson</artifactId>
5252
</dependency>
53+
<dependency>
54+
<groupId>com.auth0</groupId>
55+
<artifactId>java-jwt</artifactId>
56+
</dependency>
5357
</dependencies>
5458
</project>

oauth/src/main/java/ch/cyberduck/core/oauth/OAuth2AuthorizationService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import java.util.List;
3636
import java.util.Map;
3737

38+
import com.auth0.jwt.JWT;
39+
import com.auth0.jwt.exceptions.JWTDecodeException;
40+
import com.auth0.jwt.interfaces.DecodedJWT;
3841
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
3942
import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
4043
import com.google.api.client.auth.oauth2.BearerToken;
@@ -157,6 +160,20 @@ public OAuthTokens validate(final OAuthTokens saved) throws BackgroundException
157160
public OAuthTokens save(final OAuthTokens tokens) throws LocalAccessDeniedException {
158161
log.debug("Save new tokens {} for {}", tokens, host);
159162
credentials.setOauth(tokens).setSaved(new LoginOptions().save);
163+
try {
164+
final DecodedJWT jwt = JWT.decode(tokens.getIdToken());
165+
// Standard claims
166+
for(String claim : new String[]{"preferred_username", "email", "name", "nickname", "sub"}) {
167+
final String value = jwt.getClaim(claim).asString();
168+
if(StringUtils.isNotBlank(value)) {
169+
credentials.setUsername(value);
170+
break;
171+
}
172+
}
173+
}
174+
catch(JWTDecodeException e) {
175+
log.warn("Failure {} decoding JWT {}", e, tokens.getIdToken());
176+
}
160177
if(credentials.isSaved()) {
161178
store.save(host);
162179
}

owncloud/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@
5353
<groupId>com.fasterxml.jackson.dataformat</groupId>
5454
<artifactId>jackson-dataformat-xml</artifactId>
5555
</dependency>
56-
<dependency>
57-
<groupId>com.auth0</groupId>
58-
<artifactId>java-jwt</artifactId>
59-
</dependency>
6056
<dependency>
6157
<groupId>ch.cyberduck</groupId>
6258
<artifactId>test</artifactId>

owncloud/src/main/java/ch/cyberduck/core/owncloud/OwncloudSession.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,18 @@
1515
* GNU General Public License for more details.
1616
*/
1717

18-
import ch.cyberduck.core.Credentials;
1918
import ch.cyberduck.core.DefaultIOExceptionMappingService;
2019
import ch.cyberduck.core.Host;
2120
import ch.cyberduck.core.HostKeyCallback;
2221
import ch.cyberduck.core.ListService;
2322
import ch.cyberduck.core.LoginCallback;
24-
import ch.cyberduck.core.OAuthTokens;
2523
import ch.cyberduck.core.UrlProvider;
2624
import ch.cyberduck.core.dav.DAVClient;
27-
import ch.cyberduck.core.dav.DAVDirectoryFeature;
2825
import ch.cyberduck.core.dav.DAVSession;
2926
import ch.cyberduck.core.dav.DAVTouchFeature;
3027
import ch.cyberduck.core.exception.BackgroundException;
3128
import ch.cyberduck.core.features.AttributesFinder;
3229
import ch.cyberduck.core.features.Delete;
33-
import ch.cyberduck.core.features.Directory;
3430
import ch.cyberduck.core.features.Home;
3531
import ch.cyberduck.core.features.Lock;
3632
import ch.cyberduck.core.features.Read;
@@ -62,16 +58,12 @@
6258
import ch.cyberduck.core.tus.TusWriteFeature;
6359

6460
import org.apache.commons.lang3.ArrayUtils;
65-
import org.apache.commons.lang3.StringUtils;
6661
import org.apache.http.client.HttpResponseException;
6762
import org.apache.logging.log4j.LogManager;
6863
import org.apache.logging.log4j.Logger;
6964

7065
import java.io.IOException;
7166

72-
import com.auth0.jwt.JWT;
73-
import com.auth0.jwt.exceptions.JWTDecodeException;
74-
7567
import static ch.cyberduck.core.tus.TusCapabilities.TUS_VERSION;
7668

7769
public class OwncloudSession extends DAVSession {
@@ -104,19 +96,6 @@ protected DAVClient connect(final ProxyFinder proxy, final HostKeyCallback key,
10496
@Override
10597
public void login(final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
10698
super.login(prompt, cancel);
107-
if(host.getProtocol().isOAuthConfigurable()) {
108-
final Credentials credentials = host.getCredentials();
109-
final OAuthTokens oauth = credentials.getOauth();
110-
try {
111-
final String username = JWT.decode(oauth.getIdToken()).getClaim("preferred_username").asString();
112-
if(StringUtils.isNotBlank(username)) {
113-
credentials.setUsername(username);
114-
}
115-
}
116-
catch(JWTDecodeException e) {
117-
log.warn("Failure {} decoding JWT {}", e, oauth.getIdToken());
118-
}
119-
}
12099
try {
121100
client.execute(new OcsCapabilitiesRequest(host), new OcsCapabilitiesResponseHandler(ocs));
122101
}

0 commit comments

Comments
 (0)