AbstractAuthenticator.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.dolphinscheduler.api.security.impl;
  18. import org.apache.dolphinscheduler.api.enums.Status;
  19. import org.apache.dolphinscheduler.api.security.Authenticator;
  20. import org.apache.dolphinscheduler.api.service.SessionService;
  21. import org.apache.dolphinscheduler.api.service.UsersService;
  22. import org.apache.dolphinscheduler.api.utils.Result;
  23. import org.apache.dolphinscheduler.common.Constants;
  24. import org.apache.dolphinscheduler.common.enums.Flag;
  25. import org.apache.dolphinscheduler.dao.entity.Session;
  26. import org.apache.dolphinscheduler.dao.entity.User;
  27. import java.util.Collections;
  28. import java.util.Map;
  29. import javax.servlet.http.HttpServletRequest;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. public abstract class AbstractAuthenticator implements Authenticator {
  34. private static final Logger logger = LoggerFactory.getLogger(AbstractAuthenticator.class);
  35. @Autowired
  36. private UsersService userService;
  37. @Autowired
  38. private SessionService sessionService;
  39. /**
  40. * user login and return user in db
  41. *
  42. * @param userId user identity field
  43. * @param password user login password
  44. * @param extra extra user login field
  45. * @return user object in databse
  46. */
  47. public abstract User login(String userId, String password, String extra);
  48. @Override
  49. public Result<Map<String, String>> authenticate(String userId, String password, String extra) {
  50. Result<Map<String, String>> result = new Result<>();
  51. User user = login(userId, password, extra);
  52. if (user == null) {
  53. result.setCode(Status.USER_NAME_PASSWD_ERROR.getCode());
  54. result.setMsg(Status.USER_NAME_PASSWD_ERROR.getMsg());
  55. return result;
  56. }
  57. // check user state
  58. if (user.getState() == Flag.NO.ordinal()) {
  59. result.setCode(Status.USER_DISABLED.getCode());
  60. result.setMsg(Status.USER_DISABLED.getMsg());
  61. return result;
  62. }
  63. // create session
  64. String sessionId = sessionService.createSession(user, extra);
  65. if (sessionId == null) {
  66. result.setCode(Status.LOGIN_SESSION_FAILED.getCode());
  67. result.setMsg(Status.LOGIN_SESSION_FAILED.getMsg());
  68. return result;
  69. }
  70. logger.info("sessionId : {}", sessionId);
  71. result.setData(Collections.singletonMap(Constants.SESSION_ID, sessionId));
  72. result.setCode(Status.SUCCESS.getCode());
  73. result.setMsg(Status.LOGIN_SUCCESS.getMsg());
  74. return result;
  75. }
  76. @Override
  77. public User getAuthUser(HttpServletRequest request) {
  78. Session session = sessionService.getSession(request);
  79. if (session == null) {
  80. logger.info("session info is null ");
  81. return null;
  82. }
  83. //get user object from session
  84. return userService.queryUser(session.getUserId());
  85. }
  86. }