-
Notifications
You must be signed in to change notification settings - Fork 214
/
AzSaslSigning.java
129 lines (113 loc) · 5.03 KB
/
AzSaslSigning.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.signing;
import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
import java.util.Optional;
import org.eclipse.ditto.base.service.UriEncoding;
import org.eclipse.ditto.connectivity.model.MessageSendingFailedException;
import org.eclipse.ditto.connectivity.model.UserPasswordCredentials;
import org.eclipse.ditto.connectivity.service.messaging.amqp.AmqpConnectionSigning;
import org.eclipse.ditto.connectivity.service.messaging.httppush.HttpRequestSigning;
import org.apache.pekko.NotUsed;
import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.model.headers.HttpCredentials;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.util.ByteString;
/**
* Signing of HTTP requests and AMQP connection strings with Azure SASL.
*/
public final class AzSaslSigning implements HttpRequestSigning, AmqpConnectionSigning {
private static final String AUTH_SCHEME = "SharedAccessSignature";
private static final String AMQP_USERNAME_TRIM_PATTERN = "\\.azure-devices\\.net$";
private final String sharedKeyName;
private final ByteString sharedKey;
private final Duration ttl;
private final String endpoint;
private AzSaslSigning(final String sharedKeyName, final ByteString sharedKey,
final Duration ttl, final String endpoint) {
this.sharedKeyName = sharedKeyName;
this.sharedKey = sharedKey;
this.ttl = ttl;
this.endpoint = endpoint;
}
/**
* Create the signing algorithm of Azure SASL.
*
* @param sharedKeyName name of the shared key.
* @param sharedKey value of the shared key.
* @param ttl how long should tokens be valid after creation.
* @param endpoint value of the {@code sr} field in the signature. Default to the URI.
* @return The signing algorithm.
*/
public static AzSaslSigning of(final String sharedKeyName, final String sharedKey,
final Duration ttl, final String endpoint) {
try {
final ByteString sharedKeyBytes = ByteString.fromArray(Base64.getDecoder().decode(sharedKey));
return new AzSaslSigning(sharedKeyName, sharedKeyBytes, ttl, endpoint);
} catch (final IllegalArgumentException e) {
throw MessageSendingFailedException.newBuilder()
.message("Failed to initiate Azure SASL signing algorithm.")
.description("The shared key is not in Base64 scheme.")
.build();
}
}
@Override
public Source<HttpRequest, NotUsed> sign(final HttpRequest request, final Instant timestamp) {
final String token = getSasToken(timestamp);
final HttpCredentials credentials = HttpCredentials.create(AUTH_SCHEME, token);
final HttpRequest signedRequest = request.addCredentials(credentials);
return Source.single(signedRequest);
}
@Override
public Optional<UserPasswordCredentials> createSignedCredentials(final Instant timestamp) {
final UserPasswordCredentials credentials = UserPasswordCredentials
.newInstance(getAmqpUsername(), getAmqpPassword(timestamp));
return Optional.of(credentials);
}
/**
* Create AMQP SASL-plain "username" identifying an SAS token.
*
* @return The "username".
*/
private String getAmqpUsername() {
return sharedKeyName + "@sas.root." + endpoint.replaceAll(AMQP_USERNAME_TRIM_PATTERN, "");
}
/**
* Create AMQP SASL-plain "password" containing the SAS token.
*
* @param timestamp Timestamp at which the token is generated.
* @return The "password".
*/
private String getAmqpPassword(final Instant timestamp) {
return AUTH_SCHEME + " " + getSasToken(timestamp);
}
/**
* Get the SAS token of an Azure resource.
*
* @param timestamp Timestamp at which the token is generated.
* @return The token.
*/
private String getSasToken(final Instant timestamp) {
final long expiry = timestamp.plus(ttl).getEpochSecond();
final String sr = UriEncoding.encodeAllButUnreserved(endpoint);
final String stringToSign = sr + "\n" + expiry;
final String signature = UriEncoding.encodeAllButUnreserved(
Base64.getEncoder().encodeToString(Signing.hmacSha256(sharedKey.toArray(), stringToSign)));
return assembleToken(sr, signature, expiry, sharedKeyName);
}
private static String assembleToken(final String sr, final String signature, final long expiry, final String skn) {
return String.format("sr=%s&sig=%s&se=%d&skn=%s", sr, signature, expiry, skn);
}
}