Implement AuthServer and Client (2.1.2.2 & 3)

This commit is contained in:
Tobias Eidelpes 2021-04-29 10:45:26 +02:00
parent f3f8bcfcac
commit f5298764ba
3 changed files with 110 additions and 9 deletions

View File

@ -1,3 +1,29 @@
syntax = "proto3"; syntax = "proto3";
// TODO implement authentication service package dst.ass2.service.api.auth.proto;
option java_package = "dst.ass2.service.api.auth.proto";
option java_multiple_files = true;
message AuthenticationRequest {
string email = 1;
string password = 2;
}
message AuthenticationResponse {
string token = 1;
string status = 2;
}
message TokenValidationRequest {
string token = 1;
}
message TokenValidationResponse {
bool valid = 1;
}
service AuthService {
rpc authenticate(AuthenticationRequest) returns (AuthenticationResponse);
rpc validateToken(TokenValidationRequest) returns (TokenValidationResponse);
}

View File

@ -2,31 +2,45 @@ package dst.ass2.service.auth.client.impl;
import dst.ass2.service.api.auth.AuthenticationException; import dst.ass2.service.api.auth.AuthenticationException;
import dst.ass2.service.api.auth.NoSuchUserException; 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.AuthenticationClientProperties;
import dst.ass2.service.auth.client.IAuthenticationClient; import dst.ass2.service.auth.client.IAuthenticationClient;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
public class GrpcAuthenticationClient implements IAuthenticationClient { public class GrpcAuthenticationClient implements IAuthenticationClient {
private final ManagedChannel channel;
// TODO make use of the generated grpc sources to implement a blocking client private final AuthServiceGrpc.AuthServiceBlockingStub blockingStub;
public GrpcAuthenticationClient(AuthenticationClientProperties properties) { public GrpcAuthenticationClient(AuthenticationClientProperties properties) {
// TODO this.channel = ManagedChannelBuilder.forAddress(properties.getHost(), properties.getPort()).usePlaintext().build();
this.blockingStub = AuthServiceGrpc.newBlockingStub(this.channel);
} }
@Override @Override
public String authenticate(String email, String password) throws NoSuchUserException, AuthenticationException { public String authenticate(String email, String password) throws NoSuchUserException, AuthenticationException {
// TODO AuthenticationRequest request = AuthenticationRequest.newBuilder().setEmail(email).setPassword(password).build();
return null; 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 @Override
public boolean isTokenValid(String token) { public boolean isTokenValid(String token) {
// TODO TokenValidationRequest request = TokenValidationRequest.newBuilder().setToken(token).build();
return false; TokenValidationResponse response = blockingStub.validateToken(request);
return response.getValid();
} }
@Override @Override
public void close() { public void close() {
// TODO this.channel.shutdownNow();
} }
} }

View File

@ -0,0 +1,61 @@
package dst.ass2.service.auth.impl;
import dst.ass2.service.api.auth.AuthenticationException;
import dst.ass2.service.api.auth.IAuthenticationService;
import dst.ass2.service.api.auth.NoSuchUserException;
import dst.ass2.service.api.auth.proto.*;
import dst.ass2.service.auth.grpc.GrpcServerProperties;
import dst.ass2.service.auth.grpc.IGrpcServerRunner;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.IOException;
@Named
@Singleton
public class GrpcServerRunner extends AuthServiceGrpc.AuthServiceImplBase implements IGrpcServerRunner {
@Resource
private GrpcServerProperties properties;
@Resource
private IAuthenticationService authenticationService;
@Override
public void run() throws IOException {
int port = properties.getPort();
Server server = ServerBuilder.forPort(port)
.addService(bindService())
.build()
.start();
}
@Override
public void authenticate(AuthenticationRequest request, StreamObserver<AuthenticationResponse> responseObserver) {
AuthenticationResponse response;
try {
String token = authenticationService.authenticate(request.getEmail(), request.getPassword());
response = AuthenticationResponse.newBuilder().setToken(token).build();
} catch (NoSuchUserException e){
response = AuthenticationResponse.newBuilder().setStatus("NoSuchUserException").build();
} catch (AuthenticationException e) {
response = AuthenticationResponse.newBuilder().setStatus("AuthenticationException").build();
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
@Override
public void validateToken(TokenValidationRequest request, StreamObserver<TokenValidationResponse> responseObserver) {
TokenValidationResponse response = TokenValidationResponse.newBuilder().setValid(false).build();
if(authenticationService.isValid(request.getToken()))
response = TokenValidationResponse.newBuilder().setValid(true).build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}