org.gitlab.api.models.GitlabUser Java Examples

The following examples show how to use org.gitlab.api.models.GitlabUser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitlabApiClient.java    From nexus3-gitlabauth-plugin with MIT License 6 votes vote down vote up
private GitlabPrincipal doAuthz(String loginName, char[] token) throws GitlabAuthenticationException {
    GitlabUser gitlabUser;
    List<GitlabGroup> groups = null;
    try {
        GitlabAPI gitlabAPI = GitlabAPI.connect(configuration.getGitlabApiUrl(), String.valueOf(token));
        gitlabUser = gitlabAPI.getUser();
    } catch (Exception e) {
        throw new GitlabAuthenticationException(e);
    }

    if (gitlabUser==null || !loginName.equals(gitlabUser.getEmail())) {
        throw new GitlabAuthenticationException("Given username not found or does not match Github Username!");
    }

    GitlabPrincipal principal = new GitlabPrincipal();

    principal.setUsername(gitlabUser.getEmail());
    principal.setGroups(getGroups((gitlabUser.getUsername())));

    return principal;
}
 
Example #2
Source File: GitLabAuthenticationToken.java    From gitlab-oauth-plugin with MIT License 6 votes vote down vote up
/**
 * @since 0.21
 */
public GitLabOAuthUserDetails getUserDetails(String username) {
	GitlabUser user = loadUser(username);
	if (user != null) {
		// FIXME to implement
		List<GrantedAuthority> groups = new ArrayList<GrantedAuthority>();
		try {
			List<GitlabGroup> gitLabGroups = gitLabAPI.getGroups();
			for (GitlabGroup gitlabGroup : gitLabGroups) {
				groups.add(new GrantedAuthorityImpl(gitlabGroup.getName()));
			}
		} catch (IOException e) {
			LOGGER.log(Level.FINE, e.getMessage(), e);
		}
		return new GitLabOAuthUserDetails(user, groups.toArray(new GrantedAuthority[groups.size()]));
	}
	return null;
}
 
Example #3
Source File: GitLabAuthenticationToken.java    From gitlab-oauth-plugin with MIT License 5 votes vote down vote up
public GitlabUser loadUser(String username) {
	try {
		if (gitLabAPI != null && isAuthenticated()) {
			List<GitlabUser> users = gitLabAPI.findUsers(username);
			if (CollectionUtils.isNotEmpty(users)) {
				return users.get(0);// FIXME : find best solution
			}
		}
	} catch (IOException e) {
		LOGGER.log(Level.FINEST, e.getMessage(), e);
	}
	return null;
}
 
Example #4
Source File: GitLabOAuthUserDetails.java    From gitlab-oauth-plugin with MIT License 4 votes vote down vote up
public GitLabOAuthUserDetails(GitlabUser user, GrantedAuthority[] authorities) {
	super(user.getUsername(), "", true, true, true, true, authorities);
}
 
Example #5
Source File: GitLabAuthenticationToken.java    From gitlab-oauth-plugin with MIT License 4 votes vote down vote up
/**
 * Returns the GHMyself object from this instance.
 */
public GitlabUser getMyself() {
	return me;
}
 
Example #6
Source File: GitLabSecurityRealm.java    From gitlab-oauth-plugin with MIT License 4 votes vote down vote up
/**
 * This is where the user comes back to at the end of the OpenID redirect
 * ping-pong.
 */
public HttpResponse doFinishLogin(StaplerRequest request) throws IOException {
    String code = request.getParameter("code");

    if (StringUtils.isBlank(code)) {
        Log.info("doFinishLogin: missing code or private_token.");
        return HttpResponses.redirectToContextRoot();
    }

    String state = request.getParameter("state");

    HttpPost httpPost = new HttpPost(gitlabWebUri + "/oauth/token");
    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("client_id", clientID));
    parameters.add(new BasicNameValuePair("client_secret", clientSecret));
    parameters.add(new BasicNameValuePair("code", code));
    parameters.add(new BasicNameValuePair("grant_type", "authorization_code"));
    parameters.add(new BasicNameValuePair("redirect_uri", buildRedirectUrl(request, state)));
    httpPost.setEntity(new UrlEncodedFormEntity(parameters, StandardCharsets.UTF_8));

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpHost proxy = getProxy(httpPost);
    if (proxy != null) {
        RequestConfig config = RequestConfig.custom()
                .setProxy(proxy)
                .build();
        httpPost.setConfig(config);
    }

    org.apache.http.HttpResponse response = httpclient.execute(httpPost);

    HttpEntity entity = response.getEntity();

    String content = EntityUtils.toString(entity);

    // When HttpClient instance is no longer needed,
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.close();

    String accessToken = extractToken(content);

    if (StringUtils.isNotBlank(accessToken)) {
        // only set the access token if it exists.
        GitLabAuthenticationToken auth = new GitLabAuthenticationToken(accessToken, getGitlabApiUri(), TokenType.ACCESS_TOKEN);

        HttpSession session = request.getSession(false);
        if (session != null) {
            // avoid session fixation
            session.invalidate();
        }
        request.getSession(true);

        SecurityContextHolder.getContext().setAuthentication(auth);

        GitlabUser self = auth.getMyself();
        User user = User.current();
        if (user != null) {
            user.setFullName(self.getName());
            // Set email from gitlab only if empty
            if (!user.getProperty(Mailer.UserProperty.class).hasExplicitlyConfiguredAddress()) {
                user.addProperty(new Mailer.UserProperty(auth.getMyself().getEmail()));
            }
        }
        SecurityListener.fireAuthenticated(new GitLabOAuthUserDetails(self, auth.getAuthorities()));
    } else {
        Log.info("Gitlab did not return an access token.");
    }

    if (StringUtils.isNotBlank(state)) {
        return HttpResponses.redirectTo(state);
    }
    return HttpResponses.redirectToContextRoot();
}
 
Example #7
Source File: GitLabUserDB.java    From git-as-svn with GNU General Public License v2.0 4 votes vote down vote up
@NotNull
private User createUser(@NotNull GitlabUser user, @Nullable String password) {
  return User.create(user.getUsername(), user.getName(), user.getEmail(), user.getId().toString(), UserType.GitLab, password == null ? null : new User.LfsCredentials(user.getUsername(), password));
}