Skip to main content
summaryrefslogtreecommitdiffstats
blob: fa1eea48c4943c41e797bb1965bc9c0e8023397d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.core.server.internal;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IOseeUserInfo;
import org.eclipse.osee.framework.core.data.OseeCredential;
import org.eclipse.osee.framework.core.data.SystemUser;
import org.eclipse.osee.framework.core.exception.OseeAuthenticationException;
import org.eclipse.osee.framework.core.server.IAuthenticationManager;
import org.eclipse.osee.framework.core.server.IAuthenticationProvider;

/**
 * @author Roberto E. Escobar
 */
public class AuthenticationManager implements IAuthenticationManager {

   private final Map<String, IAuthenticationProvider> authenticationProviders;

   public AuthenticationManager() {
      this.authenticationProviders = Collections.synchronizedMap(new HashMap<String, IAuthenticationProvider>());
   }

   @Override
   public void addAuthenticationProvider(IAuthenticationProvider authenticationProvider) {
      synchronized (authenticationProviders) {
         final String providerId = authenticationProvider.getProtocol();
         if (!authenticationProviders.containsKey(providerId)) {
            authenticationProviders.put(providerId, authenticationProvider);
         }
      }
   }

   @Override
   public boolean authenticate(OseeCredential credential) throws OseeAuthenticationException {
      if (isSafeUser(credential)) {
         return true;
      } else {
         IAuthenticationProvider provider = authenticationProviders.get(credential.getAuthenticationProtocol());
         if (provider != null) {
            return provider.authenticate(credential);
         }
      }
      throw new OseeAuthenticationException(String.format("Invalid protocol [%s]",
         credential.getAuthenticationProtocol()));
   }

   @Override
   public void removeAuthenticationProvider(IAuthenticationProvider authenticationProvider) {
      synchronized (authenticationProviders) {
         authenticationProviders.remove(authenticationProvider);
      }
   }

   @Override
   public String[] getProtocols() {
      Set<String> keys = authenticationProviders.keySet();
      return keys.toArray(new String[keys.size()]);
   }

   @Override
   public IOseeUserInfo asOseeUser(OseeCredential credential) throws OseeAuthenticationException {
      if (isGuestLogin(credential)) {
         return SystemUser.Guest;
      } else if (isBootStrap(credential)) {
         return SystemUser.BootStrap;
      } else {
         IAuthenticationProvider provider = authenticationProviders.get(credential.getAuthenticationProtocol());
         if (provider != null) {
            return provider.asOseeUserId(credential);
         }
      }
      throw new OseeAuthenticationException(String.format("Invalid protocol [%s]",
         credential.getAuthenticationProtocol()));
   }

   private boolean isGuestLogin(OseeCredential credential) {
      return credential.getUserName().equals(SystemUser.Guest.getName());
   }

   private boolean isBootStrap(OseeCredential credential) {
      return credential.getUserName().equals(SystemUser.BootStrap.getName());
   }

   private boolean isSafeUser(OseeCredential credential) {
      return isGuestLogin(credential) || isBootStrap(credential);
   }

}

Back to the top