Search in sources :

Example 1 with GitLabModeInfo

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);
    }
}
Also used : GitLabModeInfo(org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo)

Example 2 with GitLabModeInfo

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);
}
Also used : GitLabServerInfo(org.finos.legend.sdlc.server.gitlab.GitLabServerInfo) GitLabApi(org.gitlab4j.api.GitLabApi) GitLabModeInfo(org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo) GitLabAuthorizerManager(org.finos.legend.sdlc.server.gitlab.auth.GitLabAuthorizerManager) GitLabApiException(org.gitlab4j.api.GitLabApiException) TestGitLabSession(org.finos.legend.sdlc.server.gitlab.auth.TestGitLabSession) GitLabAppInfo(org.finos.legend.sdlc.server.gitlab.GitLabAppInfo) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Version(org.gitlab4j.api.models.Version) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 3 with GitLabModeInfo

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));
                }
            }
        }
    }
}
Also used : TokenType(org.gitlab4j.api.Constants.TokenType) GitLabModeInfo(org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 4 with GitLabModeInfo

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;
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabModeInfo(org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo) URI(java.net.URI)

Aggregations

GitLabModeInfo (org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo)4 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)2 GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)2 GitLabApi (org.gitlab4j.api.GitLabApi)2 URI (java.net.URI)1 GitLabAppInfo (org.finos.legend.sdlc.server.gitlab.GitLabAppInfo)1 GitLabServerInfo (org.finos.legend.sdlc.server.gitlab.GitLabServerInfo)1 GitLabAuthorizerManager (org.finos.legend.sdlc.server.gitlab.auth.GitLabAuthorizerManager)1 GitLabUserContext (org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext)1 TestGitLabSession (org.finos.legend.sdlc.server.gitlab.auth.TestGitLabSession)1 TokenType (org.gitlab4j.api.Constants.TokenType)1 GitLabApiException (org.gitlab4j.api.GitLabApiException)1 Version (org.gitlab4j.api.models.Version)1