001package com.nimbusds.common.id;
002
003
004import com.unboundid.ldap.sdk.*;
005
006import java.text.ParseException;
007
008
009/**
010 * Resolves the authorisation identity (authzId) associated with a bind (auth) 
011 * request.
012 */
013public class AuthzIdResolver {
014
015        
016        /**
017         * Resolves the authzId (user) of a simple bind request.
018         *
019         * @param bindRequest The bind request, must not be {@code null}.
020         *
021         * @return The authzId, {@code null} if it couldn't be resolved.
022         */
023        public static AuthzId resolve(final SimpleBindRequest bindRequest) {
024        
025                if (bindRequest == null)
026                        throw new IllegalArgumentException("The bind request must not be null");
027        
028                DN user;
029                
030                try {
031                        user = new DN(bindRequest.getBindDN());
032                        
033                } catch (LDAPException e ) {
034                
035                        return null;
036                }
037                
038                return new AuthzId(user);
039        }
040        
041        
042        /**
043         * Resolves the authzId (user) of a plain SASL bind request. If a
044         * target user is specified, the authorisation identity is returned,
045         * else the authentication identity.
046         *
047         * @param bindRequest The bind request, must not be {@code null}.
048         *
049         * @return The authzId, {@code null} if it couldn't be resolved.
050         */
051        public static AuthzId resolve(final PLAINBindRequest bindRequest) {
052        
053                if (bindRequest == null)
054                        throw new IllegalArgumentException("The bind request must not be null");
055                
056                String user;
057                
058                if (bindRequest.getAuthorizationID() != null)
059                        user = bindRequest.getAuthorizationID();
060                else
061                        user = bindRequest.getAuthenticationID();
062                        
063                try {
064                        return AuthzId.parse(user);
065                
066                } catch (ParseException e) {
067                
068                        return null;
069                }
070        }
071        
072        
073        /**
074         * Resolves the authzId (user) of a bind request.
075         *
076         * @param bindRequest The bind request, must not be {@code null}.
077         *
078         * @return The authzId, {@code null} if it couldn't be resolved or the
079         *         bind request type is not supported.
080         */
081        public static AuthzId resolve(final BindRequest bindRequest) {
082        
083                if (bindRequest == null)
084                        throw new IllegalArgumentException("The bind request must not be null");
085                
086                if (bindRequest instanceof SimpleBindRequest)
087                        return resolve((SimpleBindRequest)bindRequest);
088                        
089                else if (bindRequest instanceof PLAINBindRequest)
090                        return resolve((PLAINBindRequest)bindRequest);
091                        
092                else
093                        return null;
094        }
095        
096        
097        
098        /**
099         * Public instantiation disabled.
100         */
101        private AuthzIdResolver() {
102        
103                // empty
104        }
105}