From d202d880477a49176f838d5689f5812495a8e5d4 Mon Sep 17 00:00:00 2001
From: Jack Magne <jmagne@localhost.localdomain>
Date: Fri, 26 Jun 2015 15:31:46 -0700
Subject: [PATCH] Fix Pin Reset tokenType resolution.

Ticket #1423 Pin reset operation using tpsclient fails.

Recently we had added a new way to resolve the profile. That new method was
not used in the PinReset Processor. This fix addresses that and allows the Pin Reset operation to complete.
---
 .../server/tps/processor/TPSPinResetProcessor.java | 42 +++++++++++++++-------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/base/tps/src/org/dogtagpki/server/tps/processor/TPSPinResetProcessor.java b/base/tps/src/org/dogtagpki/server/tps/processor/TPSPinResetProcessor.java
index 10c74ff..3494ca6 100644
--- a/base/tps/src/org/dogtagpki/server/tps/processor/TPSPinResetProcessor.java
+++ b/base/tps/src/org/dogtagpki/server/tps/processor/TPSPinResetProcessor.java
@@ -25,6 +25,7 @@ import org.dogtagpki.server.tps.channel.SecureChannel;
 import org.dogtagpki.server.tps.dbs.ActivityDatabase;
 import org.dogtagpki.server.tps.dbs.TokenRecord;
 import org.dogtagpki.server.tps.engine.TPSEngine;
+import org.dogtagpki.server.tps.mapping.BaseMappingResolver;
 import org.dogtagpki.server.tps.mapping.FilterMappingParams;
 import org.dogtagpki.tps.main.TPSException;
 import org.dogtagpki.tps.msg.BeginOpMsg;
@@ -82,31 +83,50 @@ public class TPSPinResetProcessor extends TPSProcessor {
 
         tokenRecord = isTokenRecordPresent(appletInfo);
 
-        if(tokenRecord == null) {
+        if (tokenRecord == null) {
             //We can't reset the pin of a token that does not exist.
 
             CMS.debug(method + ": Token does not exist!");
-            throw new TPSException(method + " Can't reset pin of token that does not exist ",TPSStatus.STATUS_ERROR_MAC_RESET_PIN_PDU);
+            throw new TPSException(method + " Can't reset pin of token that does not exist ",
+                    TPSStatus.STATUS_ERROR_MAC_RESET_PIN_PDU);
         }
 
         TokenStatus status = tokenRecord.getTokenStatus();
 
         CMS.debug(method + ": Token status: " + status);
 
-        if(!status.equals(TokenStatus.ACTIVE)) {
-            throw new TPSException(method + " Attempt to reset pin of token not currently active!",TPSStatus.STATUS_ERROR_MAC_RESET_PIN_PDU);
+        if (!status.equals(TokenStatus.ACTIVE)) {
+            throw new TPSException(method + " Attempt to reset pin of token not currently active!",
+                    TPSStatus.STATUS_ERROR_MAC_RESET_PIN_PDU);
 
         }
 
         session.setTokenRecord(tokenRecord);
 
-        String resolverInstName = getResolverInstanceName();
-
         String tokenType = null;
 
-        FilterMappingParams mappingParams = createFilterMappingParams(resolverInstName, appletInfo.getCUIDhexString(), appletInfo.getMSNString(),
-                appletInfo.getMajorVersion(), appletInfo.getMinorVersion());
-        CMS.debug(method + ": resolved tokenType: " + tokenType);
+        try {
+            String resolverInstName = getResolverInstanceName();
+
+            if (!resolverInstName.equals("none") && (selectedTokenType == null)) {
+                FilterMappingParams mappingParams = createFilterMappingParams(resolverInstName,
+                        appletInfo.getCUIDhexString(), appletInfo.getMSNString(),
+                        appletInfo.getMajorVersion(), appletInfo.getMinorVersion());
+                TPSSubsystem subsystem =
+                        (TPSSubsystem) CMS.getSubsystem(TPSSubsystem.ID);
+                BaseMappingResolver resolverInst =
+                        subsystem.getMappingResolverManager().getResolverInstance(resolverInstName);
+                tokenType = resolverInst.getResolvedMapping(mappingParams);
+                setSelectedTokenType(tokenType);
+                CMS.debug(method + " resolved tokenType: " + tokenType);
+            }
+        } catch (TPSException e) {
+            auditMsg = e.toString();
+            tps.tdb.tdbActivity(ActivityDatabase.OP_PIN_RESET, tokenRecord, session.getIpAddress(), auditMsg,
+                    "failure");
+
+            throw new TPSException(auditMsg, TPSStatus.STATUS_ERROR_MISCONFIGURATION);
+        }
 
         statusUpdate(15, "PROGRESS_PIN_RESET_RESOLVE_PROFILE");
 
@@ -117,10 +137,9 @@ public class TPSPinResetProcessor extends TPSProcessor {
         checkAndUpgradeApplet(appletInfo);
         appletInfo = getAppletInfo();
 
-
         //Check and upgrade keys if called for
 
-        SecureChannel channel = checkAndUpgradeSymKeys(appletInfo,tokenRecord);
+        SecureChannel channel = checkAndUpgradeSymKeys(appletInfo, tokenRecord);
         channel.externalAuthenticate();
 
         checkAndHandlePinReset(channel);
@@ -129,7 +148,6 @@ public class TPSPinResetProcessor extends TPSProcessor {
 
         CMS.debug(method + ": Token Pin successfully reset!");
 
-
     }
 
     public static void main(String[] args) {
-- 
2.1.0

