Author: prabath
Date: Thu Dec 13 03:36:01 2007
New Revision: 11084

Log:

Modified to issue OpenIdInfocards

Modified:
   
branches/solutions/identity/openid-poc/modules/identity-provider/src/main/java/org/wso2/solutions/identity/cards/CardIssuer.java

Modified: 
branches/solutions/identity/openid-poc/modules/identity-provider/src/main/java/org/wso2/solutions/identity/cards/CardIssuer.java
==============================================================================
--- 
branches/solutions/identity/openid-poc/modules/identity-provider/src/main/java/org/wso2/solutions/identity/cards/CardIssuer.java
    (original)
+++ 
branches/solutions/identity/openid-poc/modules/identity-provider/src/main/java/org/wso2/solutions/identity/cards/CardIssuer.java
    Thu Dec 13 03:36:01 2007
@@ -67,233 +67,255 @@
  */
 public class CardIssuer {
 
-    private static Log log = LogFactory.getLog(CardIssuer.class);
+       private static Log log = LogFactory.getLog(CardIssuer.class);
 
-    private static Messages messages = Messages
-            .getInstance(IdentityProviderConstants.RESOURCES);
+       private static Messages messages = Messages
+                       .getInstance(IdentityProviderConstants.RESOURCES);
 
-    private static CardIssuerConfig issuerConfig = null;
+       private static CardIssuerConfig issuerConfig = null;
 
-    private String userIdentifier = null;
+       private String userIdentifier = null;
 
-    public CardIssuer() throws IdentityProviderException {
-        issuerConfig = CardIssuerConfig.getInstance();
-    }
-
-    public Element issueCardForUsername(String username,
-            boolean requireAppliesTo) throws IdentityProviderException {
-
-        UsernamePasswordCredential passCred = new UsernamePasswordCredential();
-        userIdentifier = username;
-        passCred.setUsername(username);
-        UserCredential cred = new UserCredential(passCred);
-        return issueCard(cred, requireAppliesTo);
-
-    }
-
-    public Element issueCardForSelfIssuedCard(String username, String ppid,
-            boolean requireAppliesTo) throws IdentityProviderException {
-        SelfIssuedCredential selfCred = new SelfIssuedCredential(ppid);
-        userIdentifier = ppid;
-        UserCredential cred = new UserCredential(selfCred);
-        return issueCard(cred, requireAppliesTo);
-    }
-
-    private Element issueCard(UserCredential credential,
-            boolean requireAppliesTo) throws IdentityProviderException {
-        ServerConfiguration serverConfig = ServerConfiguration.getInstance();
-
-        try {
-            String storeFilePath = serverConfig
-                    .getFirstProperty("Security.KeyStore.Location");
-            FileInputStream is = new FileInputStream(storeFilePath);
-
-            KeyStore store = KeyStore.getInstance(serverConfig
-                    .getFirstProperty("Security.KeyStore.Type"));
-            String passwd = serverConfig
-                    .getFirstProperty("Security.KeyStore.Password");
-            store.load(is, passwd.toCharArray());
-
-            Generator gen = new Generator();
-
-            gen.setSignatureAlgorithm(issuerConfig.getSigAlgo());
-            String alias = serverConfig
-                    .getFirstProperty("Security.KeyStore.KeyAlias");
-            Certificate[] certs = store.getCertificateChain(alias);
-            gen.setCertCain(certs);
-            gen.setPrivateKey((PrivateKey) store.getKey(alias, serverConfig
-                    .getFirstProperty("Security.KeyStore.KeyPassword")
-                    .toCharArray()));
-
-            Identity id = new Identity();
-            id.setCertificate((X509Certificate) store.getCertificate(alias));
-
-            InformationCard infoCard = getInfoCard(credential, id,
-                    requireAppliesTo);
-
-            storeCard(infoCard, credential.getCredentialId());
-            Element elem = gen.signCard(infoCard);
-
-            String cardId = infoCard.getInformationCardReference().getCardId();
-            ReportAdmin.record(userIdentifier,
-                    ActionDO.ACTION_USER_DOWNLOAD_CARD, "CardId ::" + cardId);
-
-            return elem;
-        } catch (CardModelException e) {
-            throw new IdentityProviderException("cardModelError", e);
-        } catch (Exception e) {
-            throw new IdentityProviderException("keyStoreException",
-                    new String[] { serverConfig
-                            .getFirstProperty("Security.KeyStore.Location") },
-                    e);
-        }
-    }
-
-    private InformationCard getInfoCard(UserCredential credential, Identity id,
-            boolean requireAppliesTo) throws CardModelException,
-            IdentityProviderException {
-
-        InformationCard card = new InformationCard();
-
-        InformationCardReference ref = new InformationCardReference(
-                "http://identity.wso2.org/"; + UUIDGenerator.getUUID(), 1);
-        card.setInformationCardReference(ref);
-        card.setIssuer(issuerConfig.getIssuer());
-
-        Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
-        Date now = cal.getTime();
-        long lifetime = issuerConfig.getValidPeriod() * 1000l * 60l * 60l * 
24l;
-        Date exp = new Date(now.getTime() + lifetime);
-
-        if (log.isDebugEnabled()) {
-            DateFormat zulu = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
-            log.info(messages.getMessage("createdInfocardAt",
-                    new String[] { zulu.format(now) }));
-            log.info(messages.getMessage("infocardExpiresAt",
-                    new String[] { zulu.format(exp) }));
-        }
-
-        card.setTimeIssued(now);
-        card.setTimeExpires(exp);
-
-        card.setCardName("WSO2 Managed Card");
-
-        byte[] imgBytes = getCardImageBytes();
-        CardImage img = new CardImage("image/jpeg", Base64.encode(imgBytes));
-        card.setCardImage(img);
-
-        ServerConfiguration config = ServerConfiguration.getInstance();
-        String host = config.getFirstProperty("HostName");
-        String httpsPort = config.getFirstProperty("Ports.HTTPS");
-
-        EndpointReference stsEpr = null;
-        Metadata mexEpr = null;
-
-        String stsAddress = "https://"; + host + ":" + httpsPort
-                + "/wsas/services/";
-        String mexAddress = "https://"; + host + ":" + httpsPort
-                + "/wsas/services/";
-
-        if (issuerConfig.isUseSymmetricBinding()) { // TODO post beta
-            if (credential.getCredential() instanceof 
UsernamePasswordCredential) {
-                stsAddress += 
IdentityProviderConstants.SERVICE_NAME_STS_UT_SYMM;
-                mexAddress += 
IdentityProviderConstants.SERVICE_NAME_MEX_UT_SYMM
-                        + "/get";
-            } else if (credential.getCredential() instanceof 
SelfIssuedCredential) {
-                stsAddress += 
IdentityProviderConstants.SERVICE_NAME_STS_IC_SYMM;
-                mexAddress += 
IdentityProviderConstants.SERVICE_NAME_MEX_IC_SYMM
-                        + "/get";
-            }
-        } else {
-            if (credential.getCredential() instanceof 
UsernamePasswordCredential) {
-                stsAddress += IdentityProviderConstants.SERVICE_NAME_STS_UT;
-                mexAddress += IdentityProviderConstants.SERVICE_NAME_MEX_UT
-                        + "/get";
-            } else if (credential.getCredential() instanceof 
SelfIssuedCredential) {
-                stsAddress += IdentityProviderConstants.SERVICE_NAME_STS_IC;
-                mexAddress += IdentityProviderConstants.SERVICE_NAME_MEX_IC
-                        + "/get";
-            }
-        }
-
-        stsEpr = new EndpointReference(stsAddress);
-
-        mexEpr = new Metadata(mexAddress);
-
-        stsEpr.addExtensibleElement(id.serialize());
-        stsEpr.addMetaData(mexEpr.serialize());
-
-        TokenService service = new TokenService(stsEpr, credential);
-        TokenServiceList serviceList = new TokenServiceList();
-        serviceList.addTokenService(service);
-
-        card.setTokenServiceList(serviceList);
-
-        card.setSupportedTokenTypeList(issuerConfig.getTokenTypeList());
-
-        // Get the list of supported claims
-        IPPersistenceManager db = IPPersistenceManager.getPersistanceManager();
-        ClaimDO[] supportedClaims = db.getAllSupportedClaims();
-        SupportedClaimTypeList claimTypeList = new SupportedClaimTypeList();
-        for (int i = 0; i < supportedClaims.length; i++) {
-               // Right now we do not accept OpenID from the user.
-               // We generate it for him, based on his user id and store it in 
the database.
-               // But, this claim needs to be included in the InfoCard.
-            if (supportedClaims[i].isSupported() || 
supportedClaims[i].getUri().equals(IdentityConstants.CLAIM_OPENID)) {
-                SupportedClaimType claim = new SupportedClaimType(
-                        supportedClaims[i].getUri());
-                claim.setDisplayTag(supportedClaims[i].getDisplayTag());
-                claim.setDescription(supportedClaims[i].getDescription());
-                claimTypeList.addSupportedClaimType(claim);
-            }
-        }
-
-        card.setSupportedClaimTypeList(claimTypeList);
-        final RequireAppliesTo appliesTo = new RequireAppliesTo();
-        if (requireAppliesTo) {
-            card.setRequireAppliesTo(appliesTo);
-        } else {
-            appliesTo.setOptional(true);
-            card.setRequireAppliesTo(appliesTo);
-        }
-        return card;
-
-    }
-
-    private byte[] getCardImageBytes() throws CardModelException {
-        try {
-            ParameterAdmin admin = new ParameterAdmin();
-            FileInputStream is = new FileInputStream(System
-                    .getProperty(ServerConstants.WSO2WSAS_HOME)
-                    + IdentityConstants.CARD_IMAGE_PATH);
-            ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            byte[] data = new byte[1024];
-            int length = 0;
-            while (is.available() > 0) {
-                length = is.read(data);
-                baos.write(data, 0, length);
-            }
-
-            return baos.toByteArray();
-        } catch (Exception e) {
-            throw new CardModelException(e.getMessage(), e);
-        }
-    }
-
-    private void storeCard(InformationCard card, String userId)
-            throws IdentityProviderException {
-
-        IPPersistenceManager dbman = IPPersistenceManager
-                .getPersistanceManager();
-        InfoCardDO cardDo = new InfoCardDO();
-        cardDo.setCardId(card.getInformationCardReference().getCardId());
-        cardDo.setDateIssued(card.getTimeIssued());
-        cardDo.setUserId(userId);
-        cardDo.setDateExpires(card.getTimeExpires());
-
-        dbman.create(cardDo);
-        log.info("Information card details stored for card id : "
-                + card.getInformationCardReference().getCardId());
-    }
+       private boolean isOpenIdSupported = false;
+
+       public CardIssuer() throws IdentityProviderException {
+               issuerConfig = CardIssuerConfig.getInstance();
+       }
+
+       public Element issueCardForUsername(String username,
+                       boolean requireAppliesTo) throws 
IdentityProviderException {
+
+               UsernamePasswordCredential passCred = new 
UsernamePasswordCredential();
+               userIdentifier = username;
+               passCred.setUsername(username);
+               UserCredential cred = new UserCredential(passCred);
+               return issueCard(cred, requireAppliesTo);
+
+       }
+
+       public Element issueCardForSelfIssuedCard(String username, String ppid,
+                       boolean requireAppliesTo) throws 
IdentityProviderException {
+               SelfIssuedCredential selfCred = new SelfIssuedCredential(ppid);
+               userIdentifier = ppid;
+               UserCredential cred = new UserCredential(selfCred);
+               return issueCard(cred, requireAppliesTo);
+       }
+
+       private Element issueCard(UserCredential credential,
+                       boolean requireAppliesTo) throws 
IdentityProviderException {
+               ServerConfiguration serverConfig = 
ServerConfiguration.getInstance();
+
+               try {
+                       String storeFilePath = serverConfig
+                                       
.getFirstProperty("Security.KeyStore.Location");
+                       FileInputStream is = new FileInputStream(storeFilePath);
+
+                       KeyStore store = KeyStore.getInstance(serverConfig
+                                       
.getFirstProperty("Security.KeyStore.Type"));
+                       String passwd = serverConfig
+                                       
.getFirstProperty("Security.KeyStore.Password");
+                       store.load(is, passwd.toCharArray());
+
+                       Generator gen = new Generator();
+
+                       gen.setSignatureAlgorithm(issuerConfig.getSigAlgo());
+                       String alias = serverConfig
+                                       
.getFirstProperty("Security.KeyStore.KeyAlias");
+                       Certificate[] certs = store.getCertificateChain(alias);
+                       gen.setCertCain(certs);
+                       gen.setPrivateKey((PrivateKey) store.getKey(alias, 
serverConfig
+                                       
.getFirstProperty("Security.KeyStore.KeyPassword")
+                                       .toCharArray()));
+
+                       Identity id = new Identity();
+                       id.setCertificate((X509Certificate) 
store.getCertificate(alias));
+
+                       InformationCard infoCard = getInfoCard(credential, id,
+                                       requireAppliesTo);
+
+                       storeCard(infoCard, credential.getCredentialId());
+                       Element elem = gen.signCard(infoCard);
+
+                       String cardId = 
infoCard.getInformationCardReference().getCardId();
+                       ReportAdmin.record(userIdentifier,
+                                       ActionDO.ACTION_USER_DOWNLOAD_CARD, 
"CardId ::" + cardId);
+
+                       return elem;
+               } catch (CardModelException e) {
+                       throw new IdentityProviderException("cardModelError", 
e);
+               } catch (Exception e) {
+                       throw new IdentityProviderException("keyStoreException",
+                                       new String[] { serverConfig
+                                                       
.getFirstProperty("Security.KeyStore.Location") },
+                                       e);
+               }
+       }
+
+       private InformationCard getInfoCard(UserCredential credential, Identity 
id,
+                       boolean requireAppliesTo) throws CardModelException,
+                       IdentityProviderException {
+
+               InformationCard card = new InformationCard();
+
+               InformationCardReference ref = new InformationCardReference(
+                               "http://identity.wso2.org/"; + 
UUIDGenerator.getUUID(), 1);
+               card.setInformationCardReference(ref);
+               card.setIssuer(issuerConfig.getIssuer());
+
+               Calendar cal = new 
GregorianCalendar(TimeZone.getTimeZone("UTC"));
+               Date now = cal.getTime();
+               long lifetime = issuerConfig.getValidPeriod() * 1000l * 60l * 
60l * 24l;
+               Date exp = new Date(now.getTime() + lifetime);
+
+               if (log.isDebugEnabled()) {
+                       DateFormat zulu = new 
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+                       log.info(messages.getMessage("createdInfocardAt",
+                                       new String[] { zulu.format(now) }));
+                       log.info(messages.getMessage("infocardExpiresAt",
+                                       new String[] { zulu.format(exp) }));
+               }
+
+               card.setTimeIssued(now);
+               card.setTimeExpires(exp);
+
+               card.setCardName("WSO2 Managed Card");
+
+               byte[] imgBytes = getCardImageBytes();
+               CardImage img = new CardImage("image/jpeg", 
Base64.encode(imgBytes));
+               card.setCardImage(img);
+
+               ServerConfiguration config = ServerConfiguration.getInstance();
+               String host = config.getFirstProperty("HostName");
+               String httpsPort = config.getFirstProperty("Ports.HTTPS");
+
+               EndpointReference stsEpr = null;
+               Metadata mexEpr = null;
+
+               String stsAddress = "https://"; + host + ":" + httpsPort
+                               + "/wsas/services/";
+               String mexAddress = "https://"; + host + ":" + httpsPort
+                               + "/wsas/services/";
+
+               if (issuerConfig.isUseSymmetricBinding()) { // TODO post beta
+                       if (credential.getCredential() instanceof 
UsernamePasswordCredential) {
+                               stsAddress += 
IdentityProviderConstants.SERVICE_NAME_STS_UT_SYMM;
+                               mexAddress += 
IdentityProviderConstants.SERVICE_NAME_MEX_UT_SYMM
+                                               + "/get";
+                       } else if (credential.getCredential() instanceof 
SelfIssuedCredential) {
+                               stsAddress += 
IdentityProviderConstants.SERVICE_NAME_STS_IC_SYMM;
+                               mexAddress += 
IdentityProviderConstants.SERVICE_NAME_MEX_IC_SYMM
+                                               + "/get";
+                       }
+               } else {
+                       if (credential.getCredential() instanceof 
UsernamePasswordCredential) {
+                               stsAddress += 
IdentityProviderConstants.SERVICE_NAME_STS_UT;
+                               mexAddress += 
IdentityProviderConstants.SERVICE_NAME_MEX_UT
+                                               + "/get";
+                       } else if (credential.getCredential() instanceof 
SelfIssuedCredential) {
+                               stsAddress += 
IdentityProviderConstants.SERVICE_NAME_STS_IC;
+                               mexAddress += 
IdentityProviderConstants.SERVICE_NAME_MEX_IC
+                                               + "/get";
+                       }
+               }
+
+               stsEpr = new EndpointReference(stsAddress);
+
+               mexEpr = new Metadata(mexAddress);
+
+               stsEpr.addExtensibleElement(id.serialize());
+               stsEpr.addMetaData(mexEpr.serialize());
+
+               TokenService service = new TokenService(stsEpr, credential);
+               TokenServiceList serviceList = new TokenServiceList();
+               serviceList.addTokenService(service);
+
+               card.setTokenServiceList(serviceList);
+
+               card.setSupportedTokenTypeList(issuerConfig.getTokenTypeList());
+
+               // Get the list of supported claims
+               IPPersistenceManager db = 
IPPersistenceManager.getPersistanceManager();
+               ClaimDO[] supportedClaims = db.getAllSupportedClaims();
+               SupportedClaimTypeList claimTypeList = new 
SupportedClaimTypeList();
+
+               if (isOpenIdSupported) {
+                       for (int i = 0; i < supportedClaims.length; i++) {
+                               // Right now we do not accept OpenID from the 
user.
+                               // We generate it for him, based on his user id 
and store it in
+                               // the database.
+                               // But, this claim needs to be included in the 
InfoCard.
+                               if (supportedClaims[i].getUri().equals(
+                                               
IdentityConstants.CLAIM_OPENID)) {
+                                       SupportedClaimType claim = new 
SupportedClaimType(
+                                                       
supportedClaims[i].getUri());
+                                       
claim.setDisplayTag(supportedClaims[i].getDisplayTag());
+                                       
claim.setDescription(supportedClaims[i].getDescription());
+                                       
claimTypeList.addSupportedClaimType(claim);
+                               }
+                       }
+               } else {
+                       for (int i = 0; i < supportedClaims.length; i++) {
+
+                               if (supportedClaims[i].isSupported()) {
+                                       SupportedClaimType claim = new 
SupportedClaimType(
+                                                       
supportedClaims[i].getUri());
+                                       
claim.setDisplayTag(supportedClaims[i].getDisplayTag());
+                                       
claim.setDescription(supportedClaims[i].getDescription());
+                                       
claimTypeList.addSupportedClaimType(claim);
+                               }
+                       }
+               }
+
+               card.setSupportedClaimTypeList(claimTypeList);
+               final RequireAppliesTo appliesTo = new RequireAppliesTo();
+               if (requireAppliesTo) {
+                       card.setRequireAppliesTo(appliesTo);
+               } else {
+                       appliesTo.setOptional(true);
+                       card.setRequireAppliesTo(appliesTo);
+               }
+               return card;
+
+       }
+
+       private byte[] getCardImageBytes() throws CardModelException {
+               try {
+                       ParameterAdmin admin = new ParameterAdmin();
+                       FileInputStream is = new FileInputStream(System
+                                       
.getProperty(ServerConstants.WSO2WSAS_HOME)
+                                       + IdentityConstants.CARD_IMAGE_PATH);
+                       ByteArrayOutputStream baos = new 
ByteArrayOutputStream();
+                       byte[] data = new byte[1024];
+                       int length = 0;
+                       while (is.available() > 0) {
+                               length = is.read(data);
+                               baos.write(data, 0, length);
+                       }
+
+                       return baos.toByteArray();
+               } catch (Exception e) {
+                       throw new CardModelException(e.getMessage(), e);
+               }
+       }
+
+       private void storeCard(InformationCard card, String userId)
+                       throws IdentityProviderException {
+
+               IPPersistenceManager dbman = IPPersistenceManager
+                               .getPersistanceManager();
+               InfoCardDO cardDo = new InfoCardDO();
+               
cardDo.setCardId(card.getInformationCardReference().getCardId());
+               cardDo.setDateIssued(card.getTimeIssued());
+               cardDo.setUserId(userId);
+               cardDo.setDateExpires(card.getTimeExpires());
+
+               dbman.create(cardDo);
+               log.info("Information card details stored for card id : "
+                               + 
card.getInformationCardReference().getCardId());
+       }
+
+       public void setIsOpenIdSupported(boolean isSupported) {
+               this.isOpenIdSupported = isSupported;
+       }
 
 }

_______________________________________________
Identity-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/identity-dev

Reply via email to