Skip to content

Commit

Permalink
Add excluded auth for login auth service
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Jan 29, 2025
1 parent 00e9b60 commit a306f4b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 31 deletions.
40 changes: 40 additions & 0 deletions proto/src/main/proto/soulfire/login.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
syntax = "proto3";

option java_package = "com.soulfiremc.grpc.generated";
option java_multiple_files = true;

package soulfire.v1;

message LoginRequest {
string username = 1;
}

message NextAuthFlowResponse {
message EmailCode {
uint32 digits = 1;
}

message Success {
string token = 1;
}

message Failure {
string message = 1;
}

string authFlowToken = 1;
oneof next {
EmailCode email_code = 2;
Success success = 3;
Failure failure = 4;
}
}

message EmailCodeRequest {
repeated uint32 code = 1;
}

service LoginService {
rpc login(LoginRequest) returns (NextAuthFlowResponse);
rpc emailCode(EmailCodeRequest) returns (NextAuthFlowResponse);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
*/
package com.soulfiremc.server.grpc;

import com.soulfiremc.grpc.generated.LoginServiceGrpc;
import com.soulfiremc.server.user.AuthSystem;
import com.soulfiremc.server.util.RPCConstants;
import io.grpc.*;
import io.jsonwebtoken.*;

import java.util.Objects;

public class JwtServerInterceptor implements ServerInterceptor {
private final JwtParser parser;
private final AuthSystem authSystem;
Expand All @@ -36,40 +39,42 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> serverCall,
Metadata metadata,
ServerCallHandler<ReqT, RespT> serverCallHandler) {
var value = metadata.get(RPCConstants.AUTHORIZATION_METADATA_KEY);

var status = Status.OK;
if (value == null) {
status = Status.UNAUTHENTICATED.withDescription("Authorization token is missing");
} else if (!value.startsWith(RPCConstants.BEARER_TYPE)) {
status = Status.UNAUTHENTICATED.withDescription("Unknown authorization type");
} else {
Jws<Claims> claims = null;
// remove authorization type prefix
var token = value.substring(RPCConstants.BEARER_TYPE.length()).strip();
try {
// verify token signature and parse claims
claims = parser.parseSignedClaims(token);
} catch (JwtException e) {
status = Status.UNAUTHENTICATED.withDescription(e.getMessage()).withCause(e);
}
if (claims != null) {
var user = authSystem.authenticate(
claims.getPayload().getSubject(), claims.getPayload().getIssuedAt().toInstant());
if (!Objects.equals(serverCall.getMethodDescriptor().getServiceName(), LoginServiceGrpc.SERVICE_NAME)) {
var value = metadata.get(RPCConstants.AUTHORIZATION_METADATA_KEY);
if (value == null) {
status = Status.UNAUTHENTICATED.withDescription("Authorization token is missing");
} else if (!value.startsWith(RPCConstants.BEARER_TYPE)) {
status = Status.UNAUTHENTICATED.withDescription("Unknown authorization type");
} else {
Jws<Claims> claims = null;
// remove authorization type prefix
var token = value.substring(RPCConstants.BEARER_TYPE.length()).strip();
try {
// verify token signature and parse claims
claims = parser.parseSignedClaims(token);
} catch (JwtException e) {
status = Status.UNAUTHENTICATED.withDescription(e.getMessage()).withCause(e);
}
if (claims != null) {
var user = authSystem.authenticate(
claims.getPayload().getSubject(), claims.getPayload().getIssuedAt().toInstant());

if (user.isPresent()) {
// set client id into current context
return Contexts.interceptCall(
Context.current()
.withValue(
ServerRPCConstants.USER_CONTEXT_KEY,
user.get()),
serverCall,
metadata,
serverCallHandler
);
} else {
status = Status.UNAUTHENTICATED.withDescription("User not found");
if (user.isPresent()) {
// set client id into current context
return Contexts.interceptCall(
Context.current()
.withValue(
ServerRPCConstants.USER_CONTEXT_KEY,
user.get()),
serverCall,
metadata,
serverCallHandler
);
} else {
status = Status.UNAUTHENTICATED.withDescription("User not found");
}
}
}
}
Expand Down

0 comments on commit a306f4b

Please sign in to comment.