2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
2015-02-21 21:06:35 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
2015-02-21 21:06:35 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2015-02-21 21:06:35 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
2015-02-21 21:06:35 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2015-02-21 21:06:35 +01:00
|
|
|
*/
|
2020-03-23 15:35:58 +01:00
|
|
|
|
2015-02-21 21:06:35 +01:00
|
|
|
package sonia.scm.web;
|
|
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
import sonia.scm.plugin.Extension;
|
2017-01-17 15:54:32 +01:00
|
|
|
import sonia.scm.security.BearerToken;
|
2015-02-21 21:06:35 +01:00
|
|
|
|
|
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.Cookie;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2020-03-20 11:10:05 +01:00
|
|
|
|
|
|
|
|
import sonia.scm.security.SessionId;
|
2017-01-17 14:40:50 +01:00
|
|
|
import sonia.scm.util.HttpUtil;
|
2015-02-21 21:06:35 +01:00
|
|
|
|
|
|
|
|
/**
|
2019-11-13 14:03:48 +01:00
|
|
|
* Creates an {@link BearerToken} from the {@link HttpUtil#COOKIE_BEARER_AUTHENTICATION}
|
2015-04-01 10:27:38 +02:00
|
|
|
* cookie.
|
2015-02-21 21:06:35 +01:00
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
* @since 2.0.0
|
|
|
|
|
*/
|
|
|
|
|
@Extension
|
|
|
|
|
public class CookieBearerWebTokenGenerator implements WebTokenGenerator
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
2019-11-13 14:03:48 +01:00
|
|
|
* Creates an {@link BearerToken} from the {@link HttpUtil#COOKIE_BEARER_AUTHENTICATION}
|
2015-04-01 10:27:38 +02:00
|
|
|
* cookie.
|
2015-02-21 21:06:35 +01:00
|
|
|
*
|
2015-04-01 10:27:38 +02:00
|
|
|
* @param request http servlet request
|
2015-02-21 21:06:35 +01:00
|
|
|
*
|
2017-01-17 15:54:32 +01:00
|
|
|
* @return {@link BearerToken} or {@code null}
|
2015-02-21 21:06:35 +01:00
|
|
|
*/
|
|
|
|
|
@Override
|
2020-03-20 11:10:05 +01:00
|
|
|
public BearerToken createToken(HttpServletRequest request) {
|
2017-01-17 15:54:32 +01:00
|
|
|
BearerToken token = null;
|
2015-02-21 21:06:35 +01:00
|
|
|
Cookie[] cookies = request.getCookies();
|
|
|
|
|
|
2020-03-20 11:10:05 +01:00
|
|
|
if (cookies != null) {
|
|
|
|
|
for (Cookie cookie : cookies) {
|
|
|
|
|
if (HttpUtil.COOKIE_BEARER_AUTHENTICATION.equals(cookie.getName())) {
|
|
|
|
|
token = BearerToken.create(SessionId.from(request).orElse(null), cookie.getValue());
|
2015-02-21 21:06:35 +01:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
}
|