Spring Security 3.0 Google Apps Open Id Id Ind usando o OpenID4Java
-
26-09-2019 - |
Pergunta
Eu tento fazer login usando o Google Apps Open ID com a biblioteca OpenID4Java.
Descobri o serviço do usuário usando o seguinte código na classe de consumo:
try
{
discoveries = consumerManager.discover(identityUrl);
}
catch (DiscoveryException e)
{
throw new OpenIDConsumerException("Error during discovery", e);
}
DiscoveryInformation information = consumerManager.associate(discoveries);
HttpSession session = req.getSession(true);
session.setAttribute(DiscoveryInformation.class.getName(), information);
AuthRequest authReq;
try
{
authReq = consumerManager.authenticate(information, returnToUrl, realm);
// check for OpenID Simple Registration request needed
if (attributesByProvider != null || defaultAttributes != null)
{
//I set the attributes needed for getting the email of the user
}
}
catch (Exception e)
{
throw new OpenIDConsumerException("Error processing ConumerManager authentication", e);
}
return authReq.getDestinationUrl(true);
Em seguida, recebo os parâmetros da solicitação HTTP e na propriedade OpenId.claimed_id que recebo "http://domain.com/openid?id=.... "E se eu tentar verificar a resposta consumerManager.verify(receivingURL.toString(), openidResp, discovered);
Uma exceção é lançada: org.openid4java.discovery.yadis.YadisException: 0x706: GET failed on http://domain.com/openid?id=... : 404:Not Found
.
Para evitar a exceção, tentei modificar a lista de parâmetros alterando o valor "http://domain.com/openid?id=...." para "https://www.google.com/a/domain.com/openid?id=...."
// extract the receiving URL from the HTTP request
StringBuffer receivingURL = request.getRequestURL();
String queryString = request.getQueryString();
// extract the parameters from the authentication response
// (which comes in as a HTTP request from the OpenID provider)
ParameterList openidResp = new ParameterList(request.getParameterMap());
Parameter endPoint = openidResp.getParameter("openid.op_endpoint");
if (endPoint != null && endPoint.getValue().startsWith("https://www.google.com/a/"))
{
Parameter parameter = openidResp.getParameter("openid.claimed_id");
if (parameter != null)
{
String value = "https://www.google.com/a/" + parameter.getValue().replaceAll("http://", "");
openidResp.set(new Parameter("openid.claimed_id", value));
queryString = queryString.replaceAll("openid.claimed_id=http%3A%2F%2F", "openid.claimed_id=https%3A%2F%2Fwww.google.com%2Fa%2F");
}
parameter = openidResp.getParameter("openid.identity");
if (parameter != null)
{
String value = "https://www.google.com/a/" + parameter.getValue().replaceAll("http://", "");
openidResp.set(new Parameter("openid.identity", value));
queryString = queryString.replaceAll("openid.claimed_id=http%3A%2F%2F", "openid.claimed_id=https%3A%2F%2Fwww.google.com%2Fa%2F");
}
}
if ((queryString != null) && (queryString.length() > 0))
{
receivingURL.append("?").append(queryString);
}
// retrieve the previously stored discovery information
DiscoveryInformation discovered = (DiscoveryInformation) request.getSession().getAttribute(DiscoveryInformation.class.getName());
// verify the response
VerificationResult verification;
Map userDetails = new HashMap();
try
{
verification = consumerManager.verify(receivingURL.toString(), openidResp, discovered);
// check for OpenID Simple Registration request needed
if (attributesByProvider != null || defaultAttributes != null)
{
//Here I get the value of requested attributes
}
}
catch (Exception e)
{
throw new OpenIDConsumerException("Error verifying openid response", e);
}
// examine the verification result and extract the verified identifier
Identifier verified = null;
if (verification != null)
{
verified = verification.getVerifiedId();
}
OpenIDAuthenticationToken returnToken;
List attributes = null;
if (verified != null)
returnToken = new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.SUCCESS, verified.getIdentifier(), "some message", attributes);
else
{
Identifier id = discovered.getClaimedIdentifier();
return new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.FAILURE, id == null ? "Unknown" : id.getIdentifier(), "Verification status message: [" + verification.getStatusMsg() + "]", attributes);
}
Agora o método consumerManager.verify
não está jogando mais exceção, mas seu status é alterado para falhar. No log, os seguintes erros aparecem
09:46:45,424 ERROR ConsumerManager,http-80-1:1759 - No service element found to match the ClaimedID / OP-endpoint in the assertion.
09:46:45,428 ERROR ConsumerManager,http-80-1:1183 - Discovered information verification failed.
Eu vi em um fórum um problema semelhante, mas a solução foi mudar consumerManager.verify
para consumerManager.verifyNonce
. Não tenho certeza se o uso desse método não criará um problema de segurança. Você tem alguma ideia do que devo mudar para fazer do meu consumidor de identificação aberta para trabalhar com o Google Apps OpenID?
Solução
O Google Apps usa um processo de descoberta ligeiramente diferente do que é suportado na versão base do OpenID4Java. Há uma biblioteca completa em http://code.google.com/p/step2/ que você pode lutar útil (e um invólucro de nível superior em http://code.google.com/p/openid-filter/.)
Não conheço ninguém que tenha feito a integração de segurança da primavera com as classes Modified Step2, mas não deve ser muito difícil modificar o código para configurar o STEP2 adequadamente. Ele é construído no OpenID4Java e o código para escrever uma parte que confia é a mesma.