All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fujieid.jap.ids.endpoint.LoginEndpoint Maven / Gradle / Ivy

Go to download

Authorization service based on RFC6749(https://tools.ietf.org/html/rfc6749) protocol specification and OpenID Connect Core 1.0(https://openid.net/specs/openid-connect-core-1_0.html) specification

The newest version!
/*
 * Copyright (c) 2020-2040, 北京符节科技有限公司 ([email protected] & https://www.fujieid.com).
 * 

* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.gnu.org/licenses/lgpl.html *

* 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 com.fujieid.jap.ids.endpoint; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.fujieid.jap.http.JapHttpRequest; import com.fujieid.jap.http.JapHttpResponse; import com.fujieid.jap.ids.JapIds; import com.fujieid.jap.ids.exception.IdsException; import com.fujieid.jap.ids.model.ClientDetail; import com.fujieid.jap.ids.model.IdsRequestParam; import com.fujieid.jap.ids.model.IdsResponse; import com.fujieid.jap.ids.model.UserInfo; import com.fujieid.jap.ids.model.enums.ErrorResponse; import com.fujieid.jap.ids.pipeline.IdsPipeline; import com.fujieid.jap.ids.provider.IdsRequestParamProvider; import com.fujieid.jap.ids.util.EndpointUtil; import com.fujieid.jap.ids.util.OauthUtil; import com.fujieid.jap.ids.util.ObjectUtils; import java.io.IOException; import java.nio.charset.StandardCharsets; /** * Login Endpoint * * @author yadong.zhang (yadong.zhang0415(a)gmail.com) * @version 1.0.0 * @since 1.0.0 */ public class LoginEndpoint extends AbstractEndpoint { /** * 显示默认的登录页面 * * @param request current HTTP request * @param response current HTTP response * @throws IOException IOException */ public void showLoginPage(JapHttpRequest request, JapHttpResponse response) throws IOException { String loginPageHtml = generateLoginPageHtml(request); response.setContentType("text/html;charset=UTF-8"); response.setContentLength(loginPageHtml.getBytes(StandardCharsets.UTF_8).length); response.write(loginPageHtml); } private String generateLoginPageHtml(JapHttpRequest request) { StringBuilder sb = new StringBuilder(); sb.append("\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " Please sign in\n" + " \n" + " \n" + " \n" + " \n" + "

\n"); String authenticationUrl = ObjectUtils.appendIfNotEndWith(EndpointUtil.getLoginUrl(request), "?") + request.getQueryString(); sb.append("
\n") .append(" \n") .append("

\n") .append(" \n") .append(" \n").append("

\n") .append("

\n").append(" \n") .append(" \n") .append("

\n").append(" \n") .append("
\n"); sb.append("
\n"); sb.append(""); return sb.toString(); } /** * Login with account password * * @param request current HTTP request * @param response current HTTP response * @return Confirm authorization page */ public IdsResponse signin(JapHttpRequest request, JapHttpResponse response) { IdsPipeline idsSigninPipeline = JapIds.getContext().getSigninPipeline(); idsSigninPipeline = this.getUserInfoIdsPipeline(idsSigninPipeline); if (!idsSigninPipeline.preHandle(request, response)) { throw new IdsException("IdsSigninPipeline.preHandle returns false, the process is blocked."); } IdsRequestParam param = IdsRequestParamProvider.parseRequest(request); UserInfo userInfo = idsSigninPipeline.postHandle(request, response); if (null == userInfo) { String username = param.getUsername(); String password = param.getPassword(); if (ObjectUtil.hasEmpty(username, password)) { throw new IdsException(ErrorResponse.INVALID_USER_CERTIFICATE); } userInfo = JapIds.getContext().getUserService().loginByUsernameAndPassword(username, password, param.getClientId()); if (null == userInfo) { throw new IdsException(ErrorResponse.INVALID_USER_CERTIFICATE); } } JapIds.saveUserInfo(userInfo, request); ClientDetail clientDetail = JapIds.getContext().getClientDetailService().getByClientId(param.getClientId()); OauthUtil.validClientDetail(clientDetail); String redirectUri = null; // When the client supports automatic authorization, it will judge whether the {@code autoapprove} function is enabled if (null != clientDetail.getAutoApprove() && clientDetail.getAutoApprove() && StrUtil.isNotEmpty(param.getAutoapprove()) && "TRUE".equalsIgnoreCase(param.getAutoapprove())) { redirectUri = EndpointUtil.getAuthorizeAutoApproveUrl(request); } else { redirectUri = EndpointUtil.getConfirmPageUrl(request); } String fullUrl = OauthUtil.createAuthorizeUrl(redirectUri, param); return new IdsResponse() .data(fullUrl); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy