/
LedgerCallCredentials.java
39 lines (29 loc) · 1.24 KB
/
LedgerCallCredentials.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
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.ledger.api.auth.client;
import io.grpc.CallCredentials;
import io.grpc.Metadata;
import io.grpc.stub.AbstractStub;
import java.util.concurrent.Executor;
public final class LedgerCallCredentials extends CallCredentials {
private static Metadata.Key<String> header =
Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER);
public static<T extends AbstractStub<T>> T authenticatingStub(T stub, String token) {
return stub.withCallCredentials(new LedgerCallCredentials(token));
}
private final String token;
public LedgerCallCredentials(String token) {
super();
this.token = token;
}
@Override
public void applyRequestMetadata(RequestInfo requestInfo, Executor appExecutor, MetadataApplier applier) {
Metadata metadata = new Metadata();
metadata.put(LedgerCallCredentials.header, token.startsWith("Bearer ") ? token : "Bearer " + token);
applier.apply(metadata);
}
@Override
public void thisUsesUnstableApi() {
// No need to implement this, it's used as a warning from upstream
}
}