Implement AuthServer and Client (2.1.2.2 & 3)
This commit is contained in:
parent
f3f8bcfcac
commit
f5298764ba
@ -1,3 +1,29 @@
|
||||
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);
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user