Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.networknt.oauth.auth;

import com.networknt.client.ClientConfig;
import com.networknt.client.ClientRequestCarrier;
import com.networknt.client.Http2Client;
import com.networknt.cluster.Cluster;
import com.networknt.config.JsonMapper;
import com.networknt.httpstring.AttachmentConstants;
import com.networknt.oauth.security.LightPasswordCredential;
import com.networknt.server.Server;
import com.networknt.service.SingletonServiceFactory;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.tag.Tags;
import io.undertow.UndertowOptions;
import io.undertow.client.ClientConnection;
import io.undertow.client.ClientRequest;
Expand Down Expand Up @@ -82,6 +88,16 @@ public Account authenticate(String id, Credential credential) {
String message = "/portal/query?cmd=" + URLEncoder.encode(s, "UTF-8");
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(message);
request.getRequestHeaders().put(Headers.HOST, "localhost");
boolean injectOpenTracing = ClientConfig.get().isInjectOpenTracing();
if(injectOpenTracing) {
Tracer tracer = passwordCredential.getExchange().getAttachment(AttachmentConstants.EXCHANGE_TRACER);
if(tracer != null && tracer.activeSpan() != null) {
Tags.SPAN_KIND.set(tracer.activeSpan(), Tags.SPAN_KIND_CLIENT);
Tags.HTTP_METHOD.set(tracer.activeSpan(), request.getMethod().toString());
Tags.HTTP_URL.set(tracer.activeSpan(), request.getPath());
tracer.inject(tracer.activeSpan().context(), Format.Builtin.HTTP_HEADERS, new ClientRequestCarrier(request));
}
}
connection.sendRequest(request, client.createClientCallback(reference, latch));
latch.await();
int statusCode = reference.get().getResponseCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public AuthenticationMechanismOutcome authenticate(HttpServerExchange exchange,
}

IdentityManager idm = getIdentityManager(securityContext);
LightPasswordCredential credential = new LightPasswordCredential(password, clientAuthClass, userType);
LightPasswordCredential credential = new LightPasswordCredential(password, clientAuthClass, userType, exchange);
try {
final AuthenticationMechanismOutcome result;
Account account = idm.verify(userName, credential);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public AuthenticationMechanismOutcome runFormAuth(final HttpServerExchange excha
}

AuthenticationMechanismOutcome outcome = null;
LightPasswordCredential credential = new LightPasswordCredential(password.toCharArray(), clientAuthClass, userType);
LightPasswordCredential credential = new LightPasswordCredential(password.toCharArray(), clientAuthClass, userType, exchange);
try {
IdentityManager identityManager = getIdentityManager(securityContext);
Account account = identityManager.verify(userName, credential);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.networknt.oauth.security;

import com.sun.net.httpserver.HttpServer;
import io.undertow.security.idm.Credential;
import io.undertow.server.HttpServerExchange;

/**
* For the OAuth 2.0 authorization code grant type, we need to pass the client specific authentication
Expand All @@ -22,16 +24,22 @@ public class LightPasswordCredential implements Credential {
private final char[] password;
private final String clientAuthClass;
private final String userType;
private HttpServerExchange exchange;

public LightPasswordCredential(char[] password, String clientAuthClass, String userType) {
public LightPasswordCredential(char[] password, String clientAuthClass, String userType, HttpServerExchange exchange) {
this.password = password;
this.clientAuthClass = clientAuthClass;
this.userType = userType;
this.exchange = exchange;
}

public char[] getPassword() { return this.password; }

public String getClientAuthClass() { return this.clientAuthClass; }

public String getUserType() { return this.userType; }

public HttpServerExchange getExchange() {
return exchange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void testAuthenticate() {
}
Authenticator authenticator = SingletonServiceFactory.getBean(Authenticator.class, clazz);
Assert.assertTrue(authenticator != null);
Account account = authenticator.authenticate("stevehu@gmail.com", new LightPasswordCredential("123456".toCharArray(), null, null));
Account account = authenticator.authenticate("stevehu@gmail.com", new LightPasswordCredential("123456".toCharArray(), null, null, null));
Assert.assertTrue(account != null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ private Map<String, Object> handlePassword(HttpServerExchange exchange, Map<Stri
}
Authenticator authenticator = SingletonServiceFactory.getBean(Authenticator.class, clazz);

Account account = authenticator.authenticate(userId, new LightPasswordCredential(password, clientAuthClass, userType));
Account account = authenticator.authenticate(userId, new LightPasswordCredential(password, clientAuthClass, userType, exchange));
if(account == null) {
throw new ApiException(new Status(INCORRECT_PASSWORD));
} else {
Expand Down