Skip to content

Package: OAuth2SaslClient

OAuth2SaslClient

nameinstructionbranchcomplexitylinemethod
OAuth2SaslClient(Map, CallbackHandler)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
dispose()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
evaluateChallenge(byte[])
M: 74 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
getMechanismName()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getNegotiatedProperty(String)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
hasInitialResponse()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isComplete()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
unwrap(byte[], int, int)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
wrap(byte[], int, int)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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.io.IOException;
20: import java.io.UnsupportedEncodingException;
21: import java.util.Map;
22:
23: import javax.security.auth.callback.Callback;
24: import javax.security.auth.callback.CallbackHandler;
25: import javax.security.auth.callback.NameCallback;
26: import javax.security.auth.callback.PasswordCallback;
27: import javax.security.auth.callback.UnsupportedCallbackException;
28: import javax.security.sasl.SaslClient;
29: import javax.security.sasl.SaslException;
30:
31: import org.eclipse.angus.mail.util.ASCIIUtility;
32:
33: /**
34: * Jakarta Mail SASL client for OAUTH2.
35: *
36: * @see <a href="http://tools.ietf.org/html/rfc6749">
37: *        RFC 6749 - OAuth 2.0 Authorization Framework</a>
38: * @see <a href="http://tools.ietf.org/html/rfc6750">
39: *        RFC 6750 - OAuth 2.0 Authorization Framework: Bearer Token Usage</a>
40: * @author Bill Shannon
41: */
42: public class OAuth2SaslClient implements SaslClient {
43: private CallbackHandler cbh;
44: //private Map<String,?> props;        // XXX - not currently used
45: private boolean complete = false;
46:
47: public OAuth2SaslClient(Map<String,?> props, CallbackHandler cbh) {
48:         //this.props = props;
49:         this.cbh = cbh;
50: }
51:
52: @Override
53: public String getMechanismName() {
54:         return "XOAUTH2";
55: }
56:
57: @Override
58: public boolean hasInitialResponse() {
59:         return true;
60: }
61:
62: @Override
63: public byte[] evaluateChallenge(byte[] challenge) throws SaslException {
64:•        if (complete)
65:          return new byte[0];
66:
67:         NameCallback ncb = new NameCallback("User name:");
68:         PasswordCallback pcb = new PasswordCallback("OAuth token:", false);
69:         try {
70:          cbh.handle(new Callback[] { ncb, pcb });
71:         } catch (UnsupportedCallbackException ex) {
72:          throw new SaslException("Unsupported callback", ex);
73:         } catch (IOException ex) {
74:          throw new SaslException("Callback handler failed", ex);
75:         }
76:
77:         /*
78:          * The OAuth token isn't really a password, and Jakarta Mail doesn't
79:          * use char[] for passwords, so we don't worry about storing the
80:          * token in strings.
81:          */
82:         String user = ncb.getName();
83:         String token = new String(pcb.getPassword());
84:         pcb.clearPassword();
85:         String resp = "user=" + user + "\001auth=Bearer " + token + "\001\001";
86:         byte[] response;
87:         try {
88:          response = resp.getBytes("utf-8");
89:         } catch (UnsupportedEncodingException ex) {
90:          // fall back to ASCII
91:          response = ASCIIUtility.getBytes(resp);
92:         }
93:         complete = true;
94:         return response;
95: }
96:
97: @Override
98: public boolean isComplete() {
99:         return complete;
100: }
101:
102: @Override
103: public byte[] unwrap(byte[] incoming, int offset, int len)
104:                                 throws SaslException {
105:         throw new IllegalStateException("OAUTH2 unwrap not supported");
106: }
107:
108: @Override
109: public byte[] wrap(byte[] outgoing, int offset, int len)
110:                                 throws SaslException {
111:         throw new IllegalStateException("OAUTH2 wrap not supported");
112: }
113:
114: @Override
115: public Object getNegotiatedProperty(String propName) {
116:•        if (!complete)
117:          throw new IllegalStateException("OAUTH2 getNegotiatedProperty");
118:         return null;
119: }
120:
121: @Override
122: public void dispose() throws SaslException {
123: }
124: }