KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > jauth > AuthPolicy


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23   
24 package com.sun.enterprise.security.jauth;
25   
26 /*
27  * This class is used to define the message authentication policy that informs
28  * the actions of AuthModules.
29  *
30  * <p> This class is used to define source and recipient authentication
31  * policies. Source authentication is used to establish the identity of
32  * either the message sender or the party that established the message contents.
33  * Recipient authentication is used to establish the identity of the receiver
34  * of the message before it is sent.
35  *
36  * <p> This class is used used by the AuthConfig class to define the request and
37  * response authentication policies associated with Client and Server
38  * AuthModules.
39  *
40  * @version %I%, %G%
41  * @see AuthConfig
42  * @see ClientAuthModule
43  * @see ServerAuthModule
44  */

45
46 public class AuthPolicy {
47   
48     public static final int SOURCE_AUTH_NONE = 0;
49     public static final int SOURCE_AUTH_SENDER = 1;
50     public static final int SOURCE_AUTH_CONTENT = 2;
51
52     public static final String JavaDoc SENDER = "sender";
53     public static final String JavaDoc CONTENT = "content";
54     public static final String JavaDoc BEFORE_CONTENT = "before-content";
55     public static final String JavaDoc AFTER_CONTENT = "after-content";
56
57     private int authenticateSource = SOURCE_AUTH_NONE;
58     private boolean authenticateRecipient = false;
59     private boolean recipientBeforeContent = false;
60
61     private void setAuthenticationType(int sourceAuthType) {
62     switch (sourceAuthType) {
63         case SOURCE_AUTH_NONE:
64         case SOURCE_AUTH_SENDER:
65         case SOURCE_AUTH_CONTENT:
66         this.authenticateSource = sourceAuthType;
67         break;
68         default:
69         break;
70     }
71     }
72
73     public AuthPolicy() { }
74
75     public AuthPolicy( int sourceAuthenticationType,
76                boolean authenticateRecipient, boolean beforeContent ) {
77     setAuthenticationType(sourceAuthenticationType);
78     this.authenticateRecipient = authenticateRecipient;
79     this.recipientBeforeContent = beforeContent;
80     }
81
82     public void setSourceAuth(int sourceAuthenticationType) {
83     setAuthenticationType(sourceAuthenticationType);
84     }
85
86     /*
87      * Set the source of the message content authentication policy.
88      *
89      * @param required boolean value. When true authentication of the source of
90      * the message content is required. When false, content authentication
91      * will not be required and if authentication of the message sender is
92      * required it will remain so.
93      */

94     public void setContentAuth(boolean required) {
95     if (required) {
96         this.setSourceAuth(SOURCE_AUTH_CONTENT);
97     }
98     else if (!isSenderAuthRequired()) {
99         this.setSourceAuth(SOURCE_AUTH_NONE);
100     }
101     }
102
103     /*
104      * Set the message sender authentication policy.
105      *
106      * @param required boolean value. When true authentication of the message
107      * sender is required. When false, sender authentication will not be
108      * required and if authentication of the message content is required
109      * it will remain so.
110      */

111     public void setSenderAuth(boolean required) {
112     if (required) {
113         this.setSourceAuth(SOURCE_AUTH_SENDER);
114     }
115     else if (!isContentAuthRequired()) {
116         this.setSourceAuth(SOURCE_AUTH_NONE);
117     }
118     }
119
120     public void setRecipientAuth(boolean required, boolean beforeContent) {
121     this.authenticateRecipient = required;
122     this.recipientBeforeContent = beforeContent;
123     }
124
125     public int getSourceAuth() {
126     return this.authenticateSource;
127     }
128
129     public boolean authRequired() {
130     return this.isSourceAuthRequired() || this.isRecipientAuthRequired();
131     }
132
133     public boolean isSourceAuthRequired() {
134     return this.authenticateSource == 0 ? false : true;
135     }
136
137     public boolean isSenderAuthRequired() {
138     return ( this.isSourceAuthRequired() ?
139          ( this.getSourceAuth() == SOURCE_AUTH_SENDER ?
140            true : false ) : false );
141     }
142
143     public boolean isContentAuthRequired() {
144     return ( this.isSourceAuthRequired() ?
145          ( this.getSourceAuth() == SOURCE_AUTH_CONTENT ?
146            true : false ) : false );
147     }
148
149     public boolean isRecipientAuthRequired() {
150     return this.authenticateRecipient;
151     }
152
153
154     // This method interprets order from the perspective of the
155
// message sender. The value returned by this method, is only
156
// relevant when recipientAuth is required.
157
public boolean isRecipientAuthBeforeContent() {
158     return this.recipientBeforeContent;
159     }
160
161     // When orderForValidation is true, returns true if validator must
162
// validate recipient auth (e.g. decrypt) before content auth (e.g. verify
163
// signature); in which case msg sender did content auth before recipient auth.
164
// Behaves same as noArg variant when orderForValidation is false. In either
165
// case, the returned value is only relevant when recipientAuth is required.
166
public boolean isRecipientAuthBeforeContent(boolean orderForValidation) {
167     return (orderForValidation ?
168         !this.recipientBeforeContent : this.recipientBeforeContent);
169     }
170
171     public String JavaDoc toString() {
172
173     // wait for 1.5
174
// StringBuilder sb = new StringBuilder();
175
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
176     switch (authenticateSource) {
177     case SOURCE_AUTH_NONE:
178         sb.append("source-auth-type = SOURCE_AUTH_NONE");
179         break;
180     case SOURCE_AUTH_SENDER:
181         sb.append("source-auth-type = SOURCE_AUTH_SENDER");
182         break;
183     case SOURCE_AUTH_CONTENT:
184         sb.append("source-auth-type = SOURCE_AUTH_CONTENT");
185         break;
186     }
187
188     if (authenticateRecipient) {
189         sb.append("\n\tauthenticate-recipient=true" +
190             "\n\tbeforeContent=" + recipientBeforeContent);
191     } else {
192         sb.append("\n\tauthenticate-recipient=false");
193     }
194     return sb.toString();
195     }
196
197     public boolean equals(Object JavaDoc o) {
198     if (this == o) {
199         return true;
200     }
201
202     if (!(o instanceof AuthPolicy)) {
203         return false;
204     }
205
206     AuthPolicy that = (AuthPolicy)o;
207     if (this.authenticateSource == that.authenticateSource &&
208         this.authenticateRecipient == that.authenticateRecipient &&
209         this.recipientBeforeContent == that.recipientBeforeContent) {
210         return true;
211     }
212
213     return false;
214     }
215
216     public int hashCode() {
217     return authenticateSource +
218         (authenticateRecipient ? 5 : 0) +
219         (recipientBeforeContent ? 10 : 0);
220     }
221 }
222
Popular Tags