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
|
/*******************************************************************************
* Copyright (c) 2016, 2018 Red Hat.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat - Initial Contribution
*******************************************************************************/
package org.eclipse.linuxtools.internal.docker.core;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Utility methods related to the OAuth2 Autorization Framework
*/
public class OAuth2Utils {
/**
* Parses the given {@code value} similar to
* <code>Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:jboss/wildfly:pull"</code>
* to extract the <code>realm</code>, <code>service</code> and
* <code>scope</code> items.
*
* @param value
* the value to parse
* @return a map of the extracted items, or <code>null</code> if the given
* {@code value} did not match the expected pattern.
*/
public static Map<String, String> parseWwwAuthenticateHeader(
final String value) {
final Pattern authHeaderPattern = Pattern.compile(
"Bearer realm=\"(?<realm>.+)\",service=\"(?<service>.+)\",scope=\"(?<scope>.+)\""); //$NON-NLS-1$
final Matcher matcher = authHeaderPattern.matcher(value);
if (!matcher.matches()) {
return null;
}
final Map<String, String> result = new HashMap<>();
result.put("realm", matcher.group("realm")); //$NON-NLS-1$ //$NON-NLS-2$
result.put("service", matcher.group("service")); //$NON-NLS-1$ //$NON-NLS-2$
result.put("scope", matcher.group("scope")); //$NON-NLS-1$ //$NON-NLS-2$
return result;
}
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
public static class BearerTokenResponse {
@JsonProperty("token") //$NON-NLS-1$
private String token;
public String getToken() {
return this.token;
}
public void setToken(final String token) {
this.token = token;
}
}
}
|