|
| 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.seata.core.auth; |
| 18 | + |
| 19 | + |
| 20 | +import org.apache.seata.common.ConfigurationKeys; |
| 21 | +import org.apache.seata.common.util.StringUtils; |
| 22 | +import org.apache.seata.config.ConfigurationFactory; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | + |
| 26 | +public class JwtAuthManager { |
| 27 | + private String refreshToken; |
| 28 | + |
| 29 | + private String accessToken; |
| 30 | + |
| 31 | + private boolean isAccessTokenNearExpiration; |
| 32 | + |
| 33 | + private String username; |
| 34 | + |
| 35 | + private String password; |
| 36 | + |
| 37 | + public final static String PRO_USERNAME = "username"; |
| 38 | + |
| 39 | + public final static String PRO_PASSWORD = "password"; |
| 40 | + |
| 41 | + public final static String PRO_TOKEN = "token"; |
| 42 | + |
| 43 | + public final static String PRO_REFRESH_TOKEN = "refresh_token"; |
| 44 | + |
| 45 | + private static volatile JwtAuthManager instance; |
| 46 | + |
| 47 | + private JwtAuthManager() { |
| 48 | + } |
| 49 | + |
| 50 | + public static JwtAuthManager getInstance() { |
| 51 | + if (instance == null) { |
| 52 | + synchronized (JwtAuthManager.class) { |
| 53 | + if (instance == null) { |
| 54 | + instance = new JwtAuthManager(); |
| 55 | + instance.username = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(ConfigurationKeys.SECURITY_USERNME); |
| 56 | + instance.password = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(ConfigurationKeys.SECURITY_PASSWORD); |
| 57 | + instance.isAccessTokenNearExpiration = false; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return instance; |
| 62 | + } |
| 63 | + |
| 64 | + public void init() { |
| 65 | + } |
| 66 | + |
| 67 | + public boolean isAccessTokenNearExpiration() { |
| 68 | + return isAccessTokenNearExpiration; |
| 69 | + } |
| 70 | + |
| 71 | + public void setAccessTokenNearExpiration(boolean accessTokenNearExpiration) { |
| 72 | + isAccessTokenNearExpiration = accessTokenNearExpiration; |
| 73 | + } |
| 74 | + |
| 75 | + public String getAccessToken() { |
| 76 | + return accessToken; |
| 77 | + } |
| 78 | + |
| 79 | + public String getRefreshToken() { |
| 80 | + return refreshToken; |
| 81 | + } |
| 82 | + |
| 83 | + public String getUsername() { |
| 84 | + return username; |
| 85 | + } |
| 86 | + |
| 87 | + public void setUsername(String username) { |
| 88 | + this.username = username; |
| 89 | + } |
| 90 | + |
| 91 | + public String getPassword() { |
| 92 | + return password; |
| 93 | + } |
| 94 | + |
| 95 | + public void setPassword(String password) { |
| 96 | + this.password = password; |
| 97 | + } |
| 98 | + |
| 99 | + public void refreshToken(String newAccessToken, String newRefreshToken) { |
| 100 | + if (newAccessToken != null) { |
| 101 | + accessToken = newAccessToken; |
| 102 | + isAccessTokenNearExpiration = false; |
| 103 | + } |
| 104 | + if (newRefreshToken != null) { |
| 105 | + refreshToken = newRefreshToken; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public void setAccessToken(String token) { |
| 110 | + accessToken = token; |
| 111 | + } |
| 112 | + |
| 113 | + public void setRefreshToken(String token) { |
| 114 | + refreshToken = token; |
| 115 | + } |
| 116 | + |
| 117 | + public String getAuthData() { |
| 118 | + HashMap<String, String> extraDataMap = new HashMap<>(); |
| 119 | + extraDataMap.remove(PRO_TOKEN); |
| 120 | + if (accessToken != null && !isAccessTokenNearExpiration) { |
| 121 | + extraDataMap.put(PRO_TOKEN, accessToken); |
| 122 | + } else if (refreshToken != null) { |
| 123 | + extraDataMap.put(PRO_REFRESH_TOKEN, refreshToken); |
| 124 | + } else if (username != null && password != null) { |
| 125 | + extraDataMap.put(PRO_USERNAME, username); |
| 126 | + extraDataMap.put(PRO_PASSWORD, password); |
| 127 | + } |
| 128 | + return StringUtils.map2String(extraDataMap); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments