Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from erpya/feature/add-transcoding
Browse files Browse the repository at this point in the history
Add transcoding support
  • Loading branch information
yamelsenih authored Sep 26, 2023
2 parents d0ab26f + 1b1244e commit 34ace07
Show file tree
Hide file tree
Showing 15 changed files with 1,637 additions and 378 deletions.
21 changes: 21 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ protobuf {
all()*.plugins {
grpc {}
}
all().configureEach { task ->
// If true, will generate a descriptor_set.desc file under
// task.outputBaseDir. Default is false.
// See --descriptor_set_out in protoc documentation about what it is.
task.generateDescriptorSet = true

// Allows to override the default for the descriptor set location
task.descriptorSetOptions.path =
"${projectDir}/build/descriptors/adempiere-middleware.pb"

// If true, the descriptor set will contain line number information
// and comments. Default is false.
task.descriptorSetOptions.includeSourceInfo = true

// If true, the descriptor set will contain all transitive imports and
// is therefore self-contained. Default is false.
task.descriptorSetOptions.includeImports = true
}
}
}
sourceSets {
Expand Down Expand Up @@ -103,6 +121,9 @@ dependencies {
implementation "io.grpc:grpc-protobuf:${grpcVersion}"
implementation "io.grpc:grpc-stub:${grpcVersion}"
implementation "io.grpc:grpc-netty:${grpcVersion}"
// https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java
implementation 'com.google.protobuf:protobuf-java:3.24.3'
implementation 'com.google.protobuf:protobuf-java-util:3.24.3'
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.2'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
************************************************************************************/
package org.spin.authentication;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import io.grpc.Context;
import io.grpc.Contexts;
import io.grpc.Metadata;
Expand All @@ -24,22 +28,39 @@

public class AuthorizationServerInterceptor implements ServerInterceptor {

/** Threaded key for context management */
public static final Context.Key<Object> SESSION_CONTEXT = Context.key("session_context");
/** Services/Methods allow request without Bearer token validation */
private static List<String> ALLOW_REQUESTS_WITHOUT_TOKEN = Arrays.asList(
""
);

/** Revoke session */
// private static List<String> REVOKE_TOKEN_SERVICES = Arrays.asList(
// ""
// );
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
String value = metadata.get(Constants.AUTHORIZATION_METADATA_KEY);
String callingMethod = serverCall.getMethodDescriptor().getFullMethodName();
// Bypass to ingore Bearer validation
if (ALLOW_REQUESTS_WITHOUT_TOKEN.contains(callingMethod)) {
return Contexts.interceptCall(Context.current(), serverCall, metadata, serverCallHandler);
}

Status status;
if (value == null) {
Status status;
String validToken = metadata.get(Constants.AUTHORIZATION_METADATA_KEY);
if (validToken == null || validToken.trim().length() <= 0) {
status = Status.UNAUTHENTICATED.withDescription("Authorization token is missing");
} else if (!value.startsWith(Constants.BEARER_TYPE)) {
} else if (!validToken.startsWith(Constants.BEARER_TYPE)) {
status = Status.UNAUTHENTICATED.withDescription("Unknown authorization type");
} else {
try {
String token = value.substring(Constants.BEARER_TYPE.length()).trim();
// Create ADempiere session, throw a error if it not exists
SessionManager.createSessionFromToken(token);
// Context ctx = Context.current().withValue(Constants.CLIENT_ID_CONTEXT_KEY, claims.getBody().getSubject());
return Contexts.interceptCall(Context.current(), serverCall, metadata, serverCallHandler);
Properties sessioncontext = SessionManager.getSessionFromToken(validToken);
// if(REVOKE_TOKEN_SERVICES.contains(callingMethod)) {
// ;
// }
Context context = Context.current().withValue(SESSION_CONTEXT, sessioncontext);
return Contexts.interceptCall(context, serverCall, metadata, serverCallHandler);
} catch (Exception e) {
status = Status.UNAUTHENTICATED.withDescription(e.getMessage()).withCause(e);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/spin/authentication/BearerToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@ public void applyRequestMetadata(RequestInfo requestInfo, Executor executor, Met
public void thisUsesUnstableApi() {
// noop
}

public static String getTokenWithoutType(String token) {
if (token == null || token.trim().length() == 0) {
return "";
}
if (token.startsWith(Constants.BEARER_TYPE)) {
return token.substring(Constants.BEARER_TYPE.length()).trim();
}
return token;
}
}
Loading

0 comments on commit 34ace07

Please sign in to comment.