Implement AuthServer and Client (2.1.2.2 & 3)

This commit is contained in:
2021-04-29 10:45:26 +02:00
parent f3f8bcfcac
commit f5298764ba
3 changed files with 110 additions and 9 deletions
@@ -2,31 +2,45 @@ package dst.ass2.service.auth.client.impl;
import dst.ass2.service.api.auth.AuthenticationException;
import dst.ass2.service.api.auth.NoSuchUserException;
import dst.ass2.service.api.auth.proto.*;
import dst.ass2.service.auth.client.AuthenticationClientProperties;
import dst.ass2.service.auth.client.IAuthenticationClient;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
public class GrpcAuthenticationClient implements IAuthenticationClient {
// TODO make use of the generated grpc sources to implement a blocking client
private final ManagedChannel channel;
private final AuthServiceGrpc.AuthServiceBlockingStub blockingStub;
public GrpcAuthenticationClient(AuthenticationClientProperties properties) {
// TODO
this.channel = ManagedChannelBuilder.forAddress(properties.getHost(), properties.getPort()).usePlaintext().build();
this.blockingStub = AuthServiceGrpc.newBlockingStub(this.channel);
}
@Override
public String authenticate(String email, String password) throws NoSuchUserException, AuthenticationException {
// TODO
return null;
AuthenticationRequest request = AuthenticationRequest.newBuilder().setEmail(email).setPassword(password).build();
AuthenticationResponse response = blockingStub.authenticate(request);
if(response.getStatus().startsWith("NoSuchUserException"))
throw new NoSuchUserException("User with email " + email + " could not be found");
if(response.getStatus().startsWith("AuthenticationException"))
throw new AuthenticationException("Password for user with email " + email + " was incorrect");
return response.getToken();
}
@Override
public boolean isTokenValid(String token) {
// TODO
return false;
TokenValidationRequest request = TokenValidationRequest.newBuilder().setToken(token).build();
TokenValidationResponse response = blockingStub.validateToken(request);
return response.getValid();
}
@Override
public void close() {
// TODO
this.channel.shutdownNow();
}
}