Implement AuthServer and Client (2.1.2.2 & 3)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user