
org.simplejavamail.google.code.samples.oauth2.OAuth2SaslClientFactory Maven / Gradle / Ivy
Show all versions of google-oauth2-provider Show documentation
/* Copyright 2012 Google Inc., 2023 Benny Bottema
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.simplejavamail.google.code.samples.oauth2;
import javax.security.auth.callback.CallbackHandler;
import javax.security.sasl.SaslClient;
import javax.security.sasl.SaslClientFactory;
import java.util.Map;
import java.util.logging.Logger;
/**
* A SaslClientFactory that returns instances of OAuth2SaslClient.
*
* Only the "XOAUTH2" mechanism is supported. The {@code callbackHandler} is
* passed to the OAuth2SaslClient. Other parameters are ignored.
*/
public class OAuth2SaslClientFactory implements SaslClientFactory {
private static final Logger logger = Logger.getLogger(OAuth2SaslClientFactory.class.getName());
public static final String OAUTH_TOKEN_PROP = "mail.sasl.mechanisms.oauth2.oauthToken";
public SaslClient createSaslClient(String[] mechanisms, String authorizationId, String protocol, String serverName, Map props, CallbackHandler callbackHandler) {
for (String mechanism : mechanisms) {
if ("XOAUTH2".equalsIgnoreCase(mechanism)) {
return new OAuth2SaslClient((String) props.get(OAUTH_TOKEN_PROP), callbackHandler);
}
}
logger.info("Failed to match any mechanisms");
return null;
}
public String[] getMechanismNames(Map props) {
return new String[] { "XOAUTH2" };
}
}