001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: SecurityTokenFactory.java,v 1.3 2008/08/27 19:05:52 mrudul_uchil Exp $
026 *
027 */
028
029package com.sun.identity.wss.security;
030
031import com.iplanet.sso.SSOToken;
032import com.iplanet.sso.SSOException;
033
034/**
035 * This class <code>SecurityTokenFactory</code> represents a factory class to 
036 * create WS-Security tokens.
037 *
038 * This class uses pluggable <code>TokenProvider</code>s to generate the
039 * WS-Security tokens. If the <code>TokenProvider</code> is
040 * not specified, this class will use the default implementation of the 
041 * Token Provider.
042 * 
043 * @supported.all.api
044 */
045public class SecurityTokenFactory {
046
047    private TokenProvider tokenProvider = null;
048
049    /**
050     * Protected constructor.
051     */ 
052    protected SecurityTokenFactory(TokenProvider provider) {
053        this.tokenProvider = provider;   
054    }
055
056    /**
057     * Returns the Security Token factory instance. The default 
058     * <code>TokenProvider</code> is used to generate the 
059     * WS-Security tokens.
060     *
061     * @param credential The credential of the authenticated subject.
062     *
063     * @exception SecurityException if unable to create the 
064     *         the security token factory.
065     */
066    public static SecurityTokenFactory getInstance (
067                      SSOToken credential)
068        throws SecurityException {
069        try {
070            return new SecurityTokenFactory(new AMTokenProvider(credential)); 
071
072        } catch (SSOException se) {
073            WSSUtils.debug.error("SecurityTokenFactory.getInstance: " +
074            "Unable to get the factory instance", se);
075            throw new SecurityException(se.getMessage());
076        }
077    }
078
079    /**
080     * Returns the Security Token factory instance. 
081     *
082     * @param provider the token provider implementation.
083     *
084     * @exception SecurityException if unable to create the 
085     *         the security token factory.
086     */
087    public static SecurityTokenFactory getInstance (
088                    TokenProvider provider) throws SecurityException {
089
090        if(provider == null) {
091           throw new IllegalArgumentException(
092                WSSUtils.bundle.getString("nullTokenProvider"));
093        }
094        return new SecurityTokenFactory(provider);
095    }
096
097    /**
098     * Returns / Creates a security token from the security token specificaion.
099     *
100     * @param tokenSpec Security Token Specification that is needed to 
101     *        generate the security tokens.
102     *
103     * @return SecurityToken the security token.
104     *
105     * @exception SecurityException if the security token could not be
106     *            generated.
107     */
108    public SecurityToken getSecurityToken(SecurityTokenSpec tokenSpec)
109            throws SecurityException {
110
111        tokenProvider.init(tokenSpec);
112        return tokenProvider.getSecurityToken();
113    }
114
115    /**
116     * Returns the provider of this Security Token factory object.
117     *
118     * @return the token provider.
119     */
120    public TokenProvider getTokenProvider() {
121         return tokenProvider;
122    }
123
124}