-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96537df
commit 059aa30
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...edb-grpc/src/main/java/org/apache/horaedb/rpc/interceptors/AuthenticationInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2023 CeresDB Project Authors. Licensed under Apache-2.0. | ||
*/ | ||
package org.apache.horaedb.rpc.interceptors; | ||
|
||
import io.grpc.*; | ||
|
||
import java.util.Base64; | ||
|
||
public class AuthenticationInterceptor implements ClientInterceptor { | ||
private final String token; | ||
|
||
public AuthenticationInterceptor(String user, String password) { | ||
// Build token | ||
this.token = Base64.getEncoder().encodeToString((user + ":" + password).getBytes()); | ||
} | ||
|
||
@Override | ||
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method, | ||
final CallOptions callOpts, Channel next) { | ||
|
||
return new AuthenticationAttachingClientCall<>(next.newCall(method, callOpts), token); | ||
} | ||
|
||
private static final class AuthenticationAttachingClientCall<ReqT, RespT> | ||
extends ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT> { | ||
|
||
private final String token; | ||
private static final String AUTHORIZATION_HEADER = "authorization"; | ||
private static final String BASIC_PREFIX = "Basic "; | ||
|
||
private AuthenticationAttachingClientCall(ClientCall<ReqT, RespT> delegate, String token) { | ||
super(delegate); | ||
this.token = token; | ||
} | ||
|
||
@Override | ||
public void start(Listener<RespT> responseListener, Metadata headers) { | ||
headers.put(Metadata.Key.of(AUTHORIZATION_HEADER, Metadata.ASCII_STRING_MARSHALLER), BASIC_PREFIX + token); | ||
super.start(responseListener, headers); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters