-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add double token support for seata netty communication authe…
…ntication
- Loading branch information
1 parent
679f86e
commit 06cbe49
Showing
33 changed files
with
1,094 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
core/src/main/java/org/apache/seata/core/auth/AuthResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.apache.seata.core.auth; | ||
|
||
import org.apache.seata.core.protocol.ResultCode; | ||
|
||
public class AuthResult { | ||
private ResultCode resultCode; | ||
|
||
private String accessToken; | ||
|
||
private String refreshToken; | ||
|
||
public AuthResult() { | ||
} | ||
|
||
public AuthResult(AuthResultBuilder builder) { | ||
this.resultCode = builder.getResultCode(); | ||
this.accessToken = builder.getAccessToken(); | ||
this.refreshToken = builder.getRefreshToken(); | ||
} | ||
|
||
public ResultCode getResultCode() { | ||
return resultCode; | ||
} | ||
|
||
public void setResultCode(ResultCode resultCode) { | ||
this.resultCode = resultCode; | ||
} | ||
|
||
public String getAccessToken() { | ||
return accessToken; | ||
} | ||
|
||
public void setAccessToken(String accessToken) { | ||
this.accessToken = accessToken; | ||
} | ||
|
||
public String getRefreshToken() { | ||
return refreshToken; | ||
} | ||
|
||
public void setRefreshToken(String refreshToken) { | ||
this.refreshToken = refreshToken; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
core/src/main/java/org/apache/seata/core/auth/AuthResultBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.apache.seata.core.auth; | ||
|
||
import org.apache.seata.core.protocol.ResultCode; | ||
|
||
public class AuthResultBuilder { | ||
private ResultCode resultCode; | ||
private String accessToken; | ||
private String refreshToken; | ||
|
||
public ResultCode getResultCode() { | ||
return resultCode; | ||
} | ||
|
||
public String getAccessToken() { | ||
return accessToken; | ||
} | ||
|
||
public String getRefreshToken() { | ||
return refreshToken; | ||
} | ||
|
||
// 设置 resultCode | ||
public AuthResultBuilder setResultCode(ResultCode resultCode) { | ||
this.resultCode = resultCode; | ||
return this; | ||
} | ||
|
||
// 设置 accessToken | ||
public AuthResultBuilder setAccessToken(String accessToken) { | ||
this.accessToken = accessToken; | ||
return this; | ||
} | ||
|
||
// 设置 refreshToken | ||
public AuthResultBuilder setRefreshToken(String refreshToken) { | ||
this.refreshToken = refreshToken; | ||
return this; | ||
} | ||
|
||
// 构建最终的 AuthResult 对象 | ||
public AuthResult build() { | ||
return new AuthResult(this); | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
core/src/main/java/org/apache/seata/core/auth/JwtAuthManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.seata.core.auth; | ||
|
||
|
||
import org.apache.seata.common.ConfigurationKeys; | ||
import org.apache.seata.common.util.StringUtils; | ||
import org.apache.seata.config.ConfigurationFactory; | ||
|
||
import java.util.HashMap; | ||
|
||
public class JwtAuthManager { | ||
private String refreshToken; | ||
|
||
private String accessToken; | ||
|
||
private boolean isAccessTokenNearExpiration; | ||
|
||
private String username; | ||
|
||
private String password; | ||
|
||
public final static String PRO_USERNAME = "username"; | ||
|
||
public final static String PRO_PASSWORD = "password"; | ||
|
||
public final static String PRO_TOKEN = "token"; | ||
|
||
public final static String PRO_REFRESH_TOKEN = "refresh_token"; | ||
|
||
private static volatile JwtAuthManager instance; | ||
|
||
private JwtAuthManager() { | ||
} | ||
|
||
public static JwtAuthManager getInstance() { | ||
if (instance == null) { | ||
synchronized (JwtAuthManager.class) { | ||
if (instance == null) { | ||
instance = new JwtAuthManager(); | ||
instance.username = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(ConfigurationKeys.SECURITY_USERNME); | ||
instance.password = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(ConfigurationKeys.SECURITY_PASSWORD); | ||
instance.isAccessTokenNearExpiration = false; | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
public void init() { | ||
} | ||
|
||
public boolean isAccessTokenNearExpiration() { | ||
return isAccessTokenNearExpiration; | ||
} | ||
|
||
public void setAccessTokenNearExpiration(boolean accessTokenNearExpiration) { | ||
isAccessTokenNearExpiration = accessTokenNearExpiration; | ||
} | ||
|
||
public String getAccessToken() { | ||
return accessToken; | ||
} | ||
|
||
public String getRefreshToken() { | ||
return refreshToken; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public void refreshToken(String newAccessToken, String newRefreshToken) { | ||
if (newAccessToken != null) { | ||
accessToken = newAccessToken; | ||
isAccessTokenNearExpiration = false; | ||
} | ||
if (newRefreshToken != null) { | ||
refreshToken = newRefreshToken; | ||
} | ||
} | ||
|
||
public void setAccessToken(String token) { | ||
accessToken = token; | ||
} | ||
|
||
public void setRefreshToken(String token) { | ||
refreshToken = token; | ||
} | ||
|
||
public String getAuthData() { | ||
HashMap<String, String> extraDataMap = new HashMap<>(); | ||
extraDataMap.remove(PRO_TOKEN); | ||
if (accessToken != null && !isAccessTokenNearExpiration) { | ||
extraDataMap.put(PRO_TOKEN, accessToken); | ||
} else if (refreshToken != null) { | ||
extraDataMap.put(PRO_REFRESH_TOKEN, refreshToken); | ||
} else if (username != null && password != null) { | ||
extraDataMap.put(PRO_USERNAME, username); | ||
extraDataMap.put(PRO_PASSWORD, password); | ||
} | ||
return StringUtils.map2String(extraDataMap); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
core/src/main/java/org/apache/seata/core/auth/RegisterHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.apache.seata.core.auth; | ||
|
||
import io.netty.channel.Channel; | ||
import org.apache.seata.core.protocol.RegisterRMResponse; | ||
|
||
public interface RegisterHandler { | ||
/** | ||
* On a register response received. | ||
* | ||
* @param response received response message | ||
* @param channel channel of the response | ||
*/ | ||
void onRegisterResponse(RegisterRMResponse response, Channel channel, Integer rpcId); | ||
} |
Oops, something went wrong.