Skip to content

Commit e09faf3

Browse files
zhou9584Le Zhou
andauthored
[Feature] Adapt to Web App Service easyauth (#699)
<!-- Please provide brief information about the PR, what it contains & its purpose, new behaviors after the change. And let us know here if you need any help: https://github.com/microsoft/HydraLab/issues/new --> ## Description <!-- A few words to explain your changes --> ### Linked GitHub issue ID: # ## Pull Request Checklist <!-- Put an x in the boxes that apply. This is simply a reminder of what we are going to look for before merging your code. --> - [x] Tests for the changes have been added (for bug fixes / features) - [x] Code compiles correctly with all tests are passed. - [x] I've read the [contributing guide](https://github.com/microsoft/HydraLab/blob/main/CONTRIBUTING.md#making-changes-to-the-code) and followed the recommended practices. - [x] [Wikis](https://github.com/microsoft/HydraLab/wiki) or [README](https://github.com/microsoft/HydraLab/blob/main/README.md) have been reviewed and added / updated if needed (for bug fixes / features) ### Does this introduce a breaking change? *If this introduces a breaking change for Hydra Lab users, please describe the impact and migration path.* - [ ] Yes - [x] No ## How you tested it *Please make sure the change is tested, you can test it by adding UTs, do local test and share the screenshots, etc.* Please check the type of change your PR introduces: - [ ] Bugfix - [x] Feature - [ ] Technical design - [ ] Build related changes - [ ] Refactoring (no functional changes, no api changes) - [ ] Code style update (formatting, renaming) or Documentation content changes - [ ] Other (please describe): ### Feature UI screenshots or Technical design diagrams *If this is a relatively large or complex change, kick it off by drawing the tech design with PlantUML and explaining why you chose the solution you did and what alternatives you considered, etc...* --------- Co-authored-by: Le Zhou <[email protected]>
1 parent 709307d commit e09faf3

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

center/src/main/java/com/microsoft/hydralab/center/interceptor/BaseInterceptor.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,26 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
7171
if (authToken != null) {
7272
authToken = authToken.replaceAll("Bearer ", "");
7373
}
74+
75+
// For Azure AD authentication
76+
String accessToken = request.getHeader("X-MS-TOKEN-AAD-ID-TOKEN");
77+
78+
LOGGER.info("IdToken: " + request.getHeader("X-MS-TOKEN-AAD-ID-TOKEN"));
79+
LOGGER.info("AccessToken: " + request.getHeader("X-MS-TOKEN-AAD-ACCESS-TOKEN"));
80+
LOGGER.info("UserId: " + request.getHeader("X-MS-CLIENT-PRINCIPAL-ID"));
81+
LOGGER.info("UserName: " + request.getHeader("X-MS-CLIENT-PRINCIPAL-NAME"));
82+
7483
//check is ignore
7584
if (!authUtil.isIgnore(requestURI)) {
85+
//invoked by API client
86+
if (!StringUtils.isEmpty(accessToken)) {
87+
if (authTokenService.checkAADToken(accessToken)) {
88+
return true;
89+
} else {
90+
response.sendError(HttpStatus.UNAUTHORIZED.value(), "unauthorized, error authorization code");
91+
}
92+
}
93+
7694
//invoke by client
7795
if (!StringUtils.isEmpty(authToken)) {
7896
if (authTokenService.checkAuthToken(authToken)) {

center/src/main/java/com/microsoft/hydralab/center/service/AuthTokenService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.microsoft.hydralab.center.service;
55

66
import com.microsoft.hydralab.center.repository.AuthTokenRepository;
7+
import com.microsoft.hydralab.center.util.AuthUtil;
78
import com.microsoft.hydralab.common.entity.center.AuthToken;
89
import org.springframework.security.core.Authentication;
910
import org.springframework.security.core.context.SecurityContextHolder;
@@ -17,6 +18,8 @@
1718
@Service
1819
public class AuthTokenService {
1920

21+
@Resource
22+
AuthUtil authUtil;
2023
@Resource
2124
AuthTokenRepository authTokenRepository;
2225
@Resource
@@ -64,6 +67,15 @@ public boolean checkAuthToken(String authToken) {
6467
}
6568
}
6669

70+
public boolean checkAADToken(String aadToken) {
71+
Authentication authObj = securityUserService.loadUserAuthentication(authUtil.getLoginUserName(aadToken), aadToken);
72+
if (authObj == null) {
73+
return false;
74+
}
75+
SecurityContextHolder.getContext().setAuthentication(authObj);
76+
return true;
77+
}
78+
6779
public void loadDefaultUser(HttpSession session) {
6880
securityUserService.addDefaultUserSession(session);
6981
}

react/src/component/HeaderView.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default class HeaderView extends BaseView {
5151
const settings = [
5252
{ text: this.state.userInfo ? this.state.userInfo.userName : 'Loading', dialog: null },
5353
{ text: `Default Team: ${this.state.userInfo && this.state.userInfo.defaultTeamName ? this.state.userInfo.defaultTeamName : 'Loading'}`, dialog: 'changeDefaultTeamIsShown' },
54-
{ text: 'Logout', dialog: null }
54+
{ text: 'Logout', dialog: null, href: '/.auth/logout' }
5555
];
5656
const helpSettings = [
5757
{ text: 'Feedback', href: 'https://forms.office.com/r/0wnc2Sk0tp' },
@@ -136,6 +136,9 @@ export default class HeaderView extends BaseView {
136136
this.handleStatus(setting.dialog, true)
137137
this.getUserInfo()
138138
this.refreshTeamList()
139+
if (setting.href) {
140+
window.location.href = setting.href
141+
}
139142
}}>
140143
<Typography textAlign="center">{setting.text}</Typography>
141144
</MenuItem>

0 commit comments

Comments
 (0)