diff --git a/ass2-service/api/src/main/proto/dst/ass2/service/api/auth/proto/auth.proto b/ass2-service/api/src/main/proto/dst/ass2/service/api/auth/proto/auth.proto index 2c92277..0929725 100644 --- a/ass2-service/api/src/main/proto/dst/ass2/service/api/auth/proto/auth.proto +++ b/ass2-service/api/src/main/proto/dst/ass2/service/api/auth/proto/auth.proto @@ -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); +} \ No newline at end of file diff --git a/ass2-service/auth-client/src/main/java/dst/ass2/service/auth/client/impl/GrpcAuthenticationClient.java b/ass2-service/auth-client/src/main/java/dst/ass2/service/auth/client/impl/GrpcAuthenticationClient.java index 3ae5082..b3559ea 100644 --- a/ass2-service/auth-client/src/main/java/dst/ass2/service/auth/client/impl/GrpcAuthenticationClient.java +++ b/ass2-service/auth-client/src/main/java/dst/ass2/service/auth/client/impl/GrpcAuthenticationClient.java @@ -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(); } } diff --git a/ass2-service/auth/src/main/java/dst/ass2/service/auth/impl/GrpcServerRunner.java b/ass2-service/auth/src/main/java/dst/ass2/service/auth/impl/GrpcServerRunner.java new file mode 100644 index 0000000..891e12a --- /dev/null +++ b/ass2-service/auth/src/main/java/dst/ass2/service/auth/impl/GrpcServerRunner.java @@ -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 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 responseObserver) { + TokenValidationResponse response = TokenValidationResponse.newBuilder().setValid(false).build(); + + if(authenticationService.isValid(request.getToken())) + response = TokenValidationResponse.newBuilder().setValid(true).build(); + + responseObserver.onNext(response); + responseObserver.onCompleted(); + } +}