KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > pgp > PGPSecurityProvider


1 /*
2  * $Id: PGPSecurityProvider.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.extras.pgp;
12
13 import cryptix.message.Message;
14 import cryptix.message.MessageException;
15 import cryptix.message.SignedMessage;
16 import cryptix.pki.KeyBundle;
17
18 import org.mule.config.i18n.Messages;
19 import org.mule.umo.lifecycle.InitialisationException;
20 import org.mule.umo.security.SecurityException;
21 import org.mule.umo.security.UMOAuthentication;
22 import org.mule.umo.security.UMOSecurityContext;
23 import org.mule.umo.security.UMOSecurityContextFactory;
24 import org.mule.umo.security.UMOSecurityProvider;
25 import org.mule.umo.security.UnauthorisedException;
26 import org.mule.umo.security.UnknownAuthenticationTypeException;
27
28 /**
29  * @author ariva
30  */

31 public class PGPSecurityProvider implements UMOSecurityProvider
32 {
33     private String JavaDoc name = "PGPSecurityProvider";
34
35     private PGPKeyRing keyManager;
36
37     private UMOSecurityContextFactory factory;
38
39     /*
40      * (non-Javadoc)
41      *
42      * @see org.mule.umo.security.UMOSecurityProvider#setName(java.lang.String)
43      */

44     public void setName(String JavaDoc name)
45     {
46         this.name = name;
47     }
48
49     /*
50      * (non-Javadoc)
51      *
52      * @see org.mule.umo.security.UMOSecurityProvider#getName()
53      */

54     public String JavaDoc getName()
55     {
56         return name;
57     }
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.mule.umo.security.UMOSecurityProvider#authenticate(org.mule.umo.security.UMOAuthentication)
63      */

64     public UMOAuthentication authenticate(UMOAuthentication authentication) throws SecurityException JavaDoc
65     {
66         PGPAuthentication auth = (PGPAuthentication)authentication;
67
68         String JavaDoc userId = (String JavaDoc)auth.getPrincipal();
69
70         if (userId == null)
71         {
72             throw new UnauthorisedException(new org.mule.config.i18n.Message(Messages.X_IS_NULL, "UserId"));
73         }
74
75         KeyBundle userKeyBundle = keyManager.getKeyBundle(userId);
76
77         if (userKeyBundle == null)
78         {
79             throw new UnauthorisedException(new org.mule.config.i18n.Message("pgp", 1, userId));
80         }
81
82         Message msg = (Message)auth.getCredentials();
83
84         if (!((msg != null) && msg instanceof SignedMessage))
85         {
86             throw new UnauthorisedException(new org.mule.config.i18n.Message("pgp", 2));
87         }
88
89         try
90         {
91             if (!((SignedMessage)msg).verify(userKeyBundle))
92             {
93                 throw new UnauthorisedException(new org.mule.config.i18n.Message("pgp", 3));
94             }
95         }
96         catch (MessageException e)
97         {
98             throw new UnauthorisedException(new org.mule.config.i18n.Message("pgp", 4), e);
99         }
100
101         auth.setAuthenticated(true);
102         auth.setDetails(userKeyBundle);
103
104         return auth;
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see org.mule.umo.security.UMOSecurityProvider#supports(java.lang.Class)
111      */

112     public boolean supports(Class JavaDoc aClass)
113     {
114         return PGPAuthentication.class.isAssignableFrom(aClass);
115     }
116
117     /*
118      * (non-Javadoc)
119      *
120      * @see org.mule.umo.security.UMOSecurityProvider#createSecurityContext(org.mule.umo.security.UMOAuthentication)
121      */

122     public UMOSecurityContext createSecurityContext(UMOAuthentication auth)
123         throws UnknownAuthenticationTypeException
124     {
125         return factory.create(auth);
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see org.mule.umo.lifecycle.Initialisable#initialise()
132      */

133     public void initialise() throws InitialisationException
134     {
135         try
136         {
137             java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
138             java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
139
140             factory = new PGPSecurityContextFactory();
141         }
142         catch (Exception JavaDoc e)
143         {
144             throw new InitialisationException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X,
145                 "PGPProvider"), e);
146         }
147     }
148
149     public PGPKeyRing getKeyManager()
150     {
151         return keyManager;
152     }
153
154     public void setKeyManager(PGPKeyRing keyManager)
155     {
156         this.keyManager = keyManager;
157     }
158 }
159
Popular Tags