-
Notifications
You must be signed in to change notification settings - Fork 6
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
95c11a4
commit 0c772c5
Showing
13 changed files
with
226 additions
and
8 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
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
70 changes: 70 additions & 0 deletions
70
highflip-clients/highflip-sdk/src/main/java/com/baidu/highflip/client/common/BasicToken.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,70 @@ | ||
package com.baidu.highflip.client.common; | ||
|
||
import com.google.common.base.Strings; | ||
import io.grpc.CallCredentials; | ||
import io.grpc.Metadata; | ||
import io.grpc.Status; | ||
import lombok.Data; | ||
|
||
import java.util.Base64; | ||
import java.util.Optional; | ||
import java.util.concurrent.Executor; | ||
|
||
@Data | ||
public class BasicToken extends CallCredentials { | ||
|
||
public static final String BASIC_PREFIX = "Basic"; | ||
|
||
public static final String AUTHORIZATION_METADATA_KEY = "Authorization" | ||
+ Metadata.BINARY_HEADER_SUFFIX; | ||
|
||
private String value; | ||
|
||
private String user; | ||
|
||
private String token; | ||
|
||
public static Optional<BasicToken> of(String user, String token){ | ||
if(!Strings.isNullOrEmpty(user) && !Strings.isNullOrEmpty(token)){ | ||
return Optional.of(new BasicToken(user, token)); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
public BasicToken(String user, String token) { | ||
this.value = toAuthValue(user, token); | ||
this.user = user; | ||
this.token = token; | ||
} | ||
|
||
public static String toAuthValue(String user, String token){ | ||
String value = Base64.getEncoder() | ||
.encodeToString(String.format("%s:%s", user, token).getBytes()); | ||
|
||
return String.format("%s %s", BASIC_PREFIX, value); | ||
} | ||
|
||
@Override | ||
public void applyRequestMetadata( | ||
RequestInfo requestInfo, | ||
Executor executor, | ||
MetadataApplier metadataApplier) { | ||
|
||
executor.execute(() -> { | ||
try { | ||
Metadata headers = new Metadata(); | ||
headers.put(Metadata.Key.of(AUTHORIZATION_METADATA_KEY, Metadata.BINARY_BYTE_MARSHALLER), | ||
this.getValue().getBytes()); | ||
metadataApplier.apply(headers); | ||
} catch (Throwable e) { | ||
metadataApplier.fail(Status.UNAUTHENTICATED.withCause(e)); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void thisUsesUnstableApi() { | ||
// noop | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
highflip-clients/highflip-sdk/src/test/java/com/baidu/highflip/client/TestClient.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,28 @@ | ||
package com.baidu.highflip.client; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@Slf4j | ||
@Disabled | ||
public class TestClient { | ||
|
||
@Test | ||
public void testAuth(){ | ||
HighFlipClient client = new HighFlipClient(); | ||
client.connect("grpc://user:pass@127.0.0.1:8751"); | ||
|
||
var response = client.getPlatform(); | ||
log.info("platform get = {}", response); | ||
} | ||
|
||
@Test | ||
public void testGuestAuth(){ | ||
HighFlipClient client = new HighFlipClient(); | ||
client.connect("grpc://127.0.0.1:8751"); | ||
|
||
var response = client.getPlatform(); | ||
log.info("platform get = {}", response); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
highflip-clients/highflip-sdk/src/test/java/com/baidu/highflip/client/TestUrl.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 |
---|---|---|
@@ -1,13 +1,31 @@ | ||
package com.baidu.highflip.client; | ||
|
||
import com.baidu.highflip.client.common.GrpcURL; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@Slf4j | ||
public class TestUrl { | ||
|
||
@Test | ||
public void testUrl() { | ||
String url = "grpc://user:pass@127.0.0.1:8751"; | ||
|
||
GrpcURL grpc = GrpcURL.from(url); | ||
log.info("url = {}", grpc); | ||
|
||
Assertions.assertEquals("user", grpc.getUser()); | ||
Assertions.assertEquals("pass", grpc.getPass()); | ||
} | ||
|
||
@Test | ||
public void testSimpleUrl() { | ||
String url = "grpc://127.0.0.1:8751"; | ||
|
||
GrpcURL grpc = GrpcURL.from(url); | ||
log.info("url = {}", grpc); | ||
|
||
Assertions.assertEquals("127.0.0.1", grpc.getHost()); | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
highflip-core/src/main/java/com/baidu/highflip/core/engine/exception/AdaptorException.java
This file was deleted.
Oops, something went wrong.
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
58 changes: 58 additions & 0 deletions
58
highflip-server/src/main/java/com/baidu/highflip/server/config/SecurityConfig.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,58 @@ | ||
package com.baidu.highflip.server.config; | ||
|
||
import com.baidu.highflip.server.engine.HighFlipEngine; | ||
import com.baidu.highflip.server.exception.AuthenticationException; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.lognet.springboot.grpc.security.GrpcSecurity; | ||
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
|
||
import java.util.Objects; | ||
|
||
|
||
@Slf4j | ||
@ConditionalOnProperty( | ||
value="grpc.security.auth.enabled", | ||
havingValue = "true", | ||
matchIfMissing = false) | ||
@Configuration | ||
public class SecurityConfig extends GrpcSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
HighFlipEngine engine; | ||
|
||
@Override | ||
public void configure(GrpcSecurity builder) throws Exception { | ||
builder.authorizeRequests() | ||
.anyMethod().authenticated() | ||
.and() | ||
.authenticationProvider(new AuthenticationProvider() { | ||
@Override | ||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | ||
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken)authentication; | ||
|
||
boolean result = engine.validateUser( | ||
Objects.toString(auth.getPrincipal()), | ||
Objects.toString(auth.getCredentials())); | ||
|
||
if (!result) { | ||
throw new AuthenticationException(String.format("failed to authenticate user: %s", | ||
auth.getPrincipal().toString())); | ||
} | ||
return auth; | ||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> authentication) { | ||
return UsernamePasswordAuthenticationToken.class.equals(authentication); | ||
} | ||
}); | ||
} | ||
|
||
} |
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
8 changes: 8 additions & 0 deletions
8
...lip-server/src/main/java/com/baidu/highflip/server/exception/AuthenticationException.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,8 @@ | ||
package com.baidu.highflip.server.exception; | ||
|
||
public class AuthenticationException extends org.springframework.security.core.AuthenticationException { | ||
|
||
public AuthenticationException(String msg) { | ||
super(msg); | ||
} | ||
} |
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