use of org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo in project legend-sdlc by finos.
the class GitLabTokenManager method gitLabOAuthCallback.
boolean gitLabOAuthCallback(GitLabMode mode, String code) {
GitLabModeInfo modeInfo = getModeInfo(mode);
if (modeInfo == null) {
throw new IllegalStateException("Unsupported GitLab mode: " + mode);
}
synchronized (this.tokens) {
GitLabToken token = GitLabToken.newGitLabToken(TokenType.OAUTH2_ACCESS, GitLabOAuthAuthenticator.newAuthenticator(modeInfo).getOAuthTokenFromAuthCode(code));
GitLabToken oldToken = this.tokens.put(mode, token);
return !token.equals(oldToken);
}
}
use of org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo in project legend-sdlc by finos.
the class GitLabApiTestSetupUtil method prepareGitLabUserContextHelper.
/**
* Authenticates to GitLab and creates a test GitLabUserContext.
*
* @param username the name of user for whom we create this context.
* @param password the password of user for whom we create this context.
* @param hostUrl the url of the test host.
* @param hostScheme the scheme of the test host.
* @param hostHost the test host.
* @param hostPort the port (if necessary) of the test host.
*/
public static GitLabUserContext prepareGitLabUserContextHelper(String username, String password, String hostUrl, String hostScheme, String hostHost, Integer hostPort) throws LegendSDLCServerException {
GitLabMode gitLabMode = GitLabMode.PROD;
TestHttpServletRequest httpServletRequest = new TestHttpServletRequest();
TestGitLabSession session = new TestGitLabSession(username);
GitLabApi oauthGitLabApi;
Version version;
try {
oauthGitLabApi = GitLabApi.oauth2Login(hostUrl, username, password, null, null, true);
Assert.assertNotNull(oauthGitLabApi);
version = oauthGitLabApi.getVersion();
} catch (GitLabApiException e) {
StringBuilder builder = new StringBuilder("Error instantiating GitLabApi via OAuth2; response status: ").append(e.getHttpStatus());
StringTools.appendThrowableMessageIfPresent(builder, e, "; error message: ");
if (e.hasValidationErrors()) {
builder.append("; validation error(s): ").append(e.getValidationErrors());
}
throw new LegendSDLCServerException(builder.toString(), e);
}
String oauthToken = oauthGitLabApi.getAuthToken();
LOGGER.info("Retrieved access token: {}", oauthToken);
Assert.assertNotNull(version);
GitLabServerInfo gitLabServerInfo = GitLabServerInfo.newServerInfo(hostScheme, hostHost, hostPort);
GitLabAppInfo gitLabAppInfo = GitLabAppInfo.newAppInfo(gitLabServerInfo, null, null, null);
GitLabModeInfo gitLabModeInfo = GitLabModeInfo.newModeInfo(gitLabMode, gitLabAppInfo);
session.setGitLabToken(gitLabMode, oauthToken, TokenType.OAUTH2_ACCESS);
session.setModeInfo(gitLabModeInfo);
LegendSDLCWebFilter.setSessionAttributeOnServletRequest(httpServletRequest, session);
GitLabAuthorizerManager authorizerManager = GitLabAuthorizerManager.newManager(Collections.emptyList());
return new GitLabUserContext(httpServletRequest, null, authorizerManager);
}
use of org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo in project legend-sdlc by finos.
the class GitLabTokenManager method putAllFromToken.
void putAllFromToken(Token.TokenReader reader) {
synchronized (this.tokens) {
for (int size = reader.getInt(); size > 0; size--) {
// Read values
String modeName = reader.getString();
String appId = reader.getString();
String typeName = reader.getString();
String token = reader.getString();
if ((modeName != null) && (appId != null) && (typeName != null) && (token != null)) {
// Get mode and token type
GitLabMode mode;
TokenType type;
try {
mode = GitLabMode.valueOf(modeName);
type = TokenType.valueOf(typeName);
} catch (IllegalArgumentException e) {
// unknown mode or token type - token will be ignored
continue;
}
// Check the mode info
GitLabModeInfo modeInfo = this.modeInfos.getModeInfo(mode);
if ((modeInfo != null) && appId.equals(modeInfo.getAppInfo().getAppId())) {
this.tokens.put(mode, GitLabToken.newGitLabToken(type, token));
}
}
}
}
}
use of org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo in project legend-sdlc by finos.
the class GitLabUserContext method getGitLabAPI.
public GitLabApi getGitLabAPI(GitLabMode mode, boolean redirectAllowed) {
GitLabApi api = this.apiCache.get(mode);
if (api == null) {
if (!isValidMode(mode)) {
throw new LegendSDLCServerException("GitLab mode " + mode + " is not supported", Status.BAD_REQUEST);
}
GitLabSession gitLabSession = getGitLabSession();
synchronized (this.apiCache) {
api = this.apiCache.get(mode);
if (api == null) {
GitLabModeInfo modeInfo = gitLabSession.getModeInfo(mode);
GitLabToken token = gitLabSession.getGitLabToken(mode);
if (token == null) {
try {
token = authorizerManager.authorize(session, modeInfo);
} catch (GitLabOAuthAuthenticator.UserInputRequiredException e) {
URI redirectURI = GitLabOAuthAuthenticator.buildAppAuthorizationURI(e.getModeInfo(), this.httpRequest);
throw new LegendSDLCServerException(redirectURI.toString(), Status.FOUND);
} catch (GitLabAuthFailureException e) {
throw new LegendSDLCServerException(e.getMessage(), Status.FORBIDDEN, e);
} catch (GitLabAuthException e) {
throw new LegendSDLCServerException(e.getMessage(), Status.INTERNAL_SERVER_ERROR, e);
}
if (token != null) {
gitLabSession.putGitLabToken(mode, token);
LegendSDLCWebFilter.setSessionCookie(this.httpResponse, gitLabSession);
} else if (redirectAllowed) {
URI redirectURI = GitLabOAuthAuthenticator.buildAppAuthorizationURI(modeInfo, this.httpRequest);
throw new LegendSDLCServerException(redirectURI.toString(), Status.FOUND);
} else {
throw new LegendSDLCServerException("{\"message\":\"Authorization required\",\"auth_uri\":\"/auth/authorize\"}", Status.FORBIDDEN);
}
}
api = new GitLabApi(ApiVersion.V4, modeInfo.getServerInfo().getGitLabURLString(), token.getTokenType(), token.getToken());
this.apiCache.put(mode, api);
}
}
}
return api;
}
Aggregations