Package: OAuth2SaslClientFactory$OAuth2Provider
OAuth2SaslClientFactory$OAuth2Provider
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
OAuth2SaslClientFactory.OAuth2Provider() |
|
|
|
|
|
Coverage
1: /*
2: * Copyright (c) 2014, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.auth;
18:
19: import java.security.Provider;
20: import java.security.Security;
21: import java.util.Map;
22:
23: import javax.security.auth.callback.CallbackHandler;
24: import javax.security.sasl.SaslClient;
25: import javax.security.sasl.SaslClientFactory;
26: import javax.security.sasl.SaslException;
27:
28: /**
29: * Jakarta Mail SASL client factory for OAUTH2.
30: *
31: * @author Bill Shannon
32: */
33: public class OAuth2SaslClientFactory implements SaslClientFactory {
34:
35: private static final String PROVIDER_NAME = "Jakarta-Mail-OAuth2";
36: private static final String MECHANISM_NAME = "SaslClientFactory.XOAUTH2";
37:
38: static class OAuth2Provider extends Provider {
39:         private static final long serialVersionUID = -5371795551562287059L;
40:
41:         public OAuth2Provider() {
42:          super(PROVIDER_NAME, 1.0, "XOAUTH2 SASL Mechanism");
43:          put(MECHANISM_NAME, OAuth2SaslClientFactory.class.getName());
44:         }
45: }
46:
47: /**
48: * Creates a default {@code OAuth2SaslClientFactory}.
49: *
50: * @see #init()
51: */
52: public OAuth2SaslClientFactory() {
53: }
54:
55: @Override
56: public SaslClient createSaslClient(String[] mechanisms,
57:                                 String authorizationId, String protocol,
58:                                 String serverName, Map<String,?> props,
59:                                 CallbackHandler cbh) throws SaslException {
60:         for (String m : mechanisms) {
61:          if (m.equals("XOAUTH2"))
62:                 return new OAuth2SaslClient(props, cbh);
63:         }
64:         return null;
65: }
66:
67: @Override
68: public String[] getMechanismNames(Map<String,?> props) {
69:         return new String[] { "XOAUTH2" };
70: }
71:
72: /**
73: * Initialize this OAUTH2 provider, but only if there isn't one already.
74: * If we're not allowed to add this provider, just give up silently.
75: */
76: public static void init() {
77:         try {
78:          if (Security.getProvider(PROVIDER_NAME) == null)
79:                 Security.addProvider(new OAuth2Provider());
80:         } catch (SecurityException ex) {
81:          // oh well...
82:         }
83: }
84: }