Skip to content
Snippets Groups Projects
Commit 5d5dd2a7 authored by Kenneth Ezekiel's avatar Kenneth Ezekiel
Browse files

add: Auth

parent 80fec692
No related merge requests found
package com.letterpaw.soap.util;
import com.letterpaw.soap.enums.ServiceType;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import java.util.List;
import java.util.Map;
public class Auth {
public static final String RESTAPIKey = System.getenv().getOrDefault("REST_API_KEY", "DefaultRESTAPIKey");
public static final String PHPAPIKey = System.getenv().getOrDefault("PHP_API_KEY", "DefaultPHPAPIKey");
public static boolean IsAuthenticated(WebServiceContext ctx, ServiceType type) {
@SuppressWarnings("unchecked")
// Check if the service is authenticated or not, message context is used to get HTTP request headers, which contains the key,
// which is then compared with the actual key
Map<String, List<String>> headers = (Map<String, List<String>>)
ctx.getMessageContext().get(MessageContext.HTTP_REQUEST_HEADERS);
// Get header for authorization
List<String> auth = headers.get("authorization");
String key = "";
// Get the stored key
if (type == ServiceType.REST) {
key = RESTAPIKey;
} else if (type == ServiceType.PHP) {
key = PHPAPIKey;
}
// Check the key validity
return !auth.isEmpty() && auth.get(0).equals("Bearer " + key);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment