Search in sources :

Example 31 with User

use of io.pivotal.cla.data.User in project pivotal-cla by pivotalsoftware.

the class OAuthController method oauth.

@RequestMapping("/login/oauth2/github")
public void oauth(ImportedSignaturesSessionAttr importedSignaturesAttr, HttpServletRequest request, HttpServletResponse response, @RequestParam String code, @RequestParam String state) throws Exception {
    String actualState = (String) request.getSession().getAttribute("state");
    if (actualState == null || !actualState.equals(state)) {
        throw new InvalidSecretState();
    }
    boolean admin = GitHubAuthenticationEntryPoint.isAdmin(state);
    OAuthAccessTokenParams params = new OAuthAccessTokenParams();
    params.setCallbackUrl(UrlBuilder.fromRequest(request).callbackUrl());
    params.setCode(code);
    params.setState(actualState);
    CurrentUserRequest userRequest = new CurrentUserRequest();
    userRequest.setOauthParams(params);
    userRequest.setRequestAdminAccess(admin);
    User user = gitHub.getCurrentUser(userRequest);
    User existingUser = users.findOne(user.getGitHubLogin());
    boolean isNewUser = existingUser == null;
    users.save(user);
    Authentication authentication = Login.loginAs(user);
    if (isNewUser) {
        List<IndividualSignature> individualSignatures = individual.findSignaturesFor(new PageRequest(0, 1), user);
        boolean signed = !individualSignatures.isEmpty();
        if (!signed) {
            List<String> organizations = gitHub.getOrganizations(user.getGitHubLogin());
            signed = !corporate.findSignatures(new PageRequest(0, 1), organizations, user.getEmails()).isEmpty();
        }
        if (signed) {
            importedSignaturesAttr.setValue(true);
        }
    }
    success.onAuthenticationSuccess(request, response, authentication);
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) User(io.pivotal.cla.data.User) Authentication(org.springframework.security.core.Authentication) CurrentUserRequest(io.pivotal.cla.service.github.CurrentUserRequest) OAuthAccessTokenParams(io.pivotal.cla.service.github.OAuthAccessTokenParams) IndividualSignature(io.pivotal.cla.data.IndividualSignature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

User (io.pivotal.cla.data.User)31 Test (org.junit.Test)22 WithSigningUser (io.pivotal.cla.security.WithSigningUser)11 AccessToken (io.pivotal.cla.data.AccessToken)9 CurrentUserRequest (io.pivotal.cla.service.github.CurrentUserRequest)8 PullRequestStatus (io.pivotal.cla.service.github.PullRequestStatus)6 SignClaPage (io.pivotal.cla.webdriver.pages.SignClaPage)4 AdminLinkClaPage (io.pivotal.cla.webdriver.pages.admin.AdminLinkClaPage)4 EnqueueRequests (okhttp3.mockwebserver.EnqueueRequests)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 UserAuthentication (io.pivotal.cla.security.Login.UserAuthentication)3 OAuthAccessTokenParams (io.pivotal.cla.service.github.OAuthAccessTokenParams)3 Matchers.anyString (org.mockito.Matchers.anyString)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 WithAnonymousUser (org.springframework.security.test.context.support.WithAnonymousUser)3 IndividualSignature (io.pivotal.cla.data.IndividualSignature)1 PullRequestId (io.pivotal.cla.egit.github.core.PullRequestId)1 WithAdminUser (io.pivotal.cla.security.WithAdminUser)1 ContributingUrlsResponse (io.pivotal.cla.service.github.ContributingUrlsResponse)1 CreatePullRequestHookRequest (io.pivotal.cla.service.github.CreatePullRequestHookRequest)1