Search in sources :

Example 1 with Identity

use of org.gitlab4j.api.models.Identity in project catma by forTEXT.

the class GitlabManagerPrivileged method createUser.

// it's more convenient to work with the User class internally, which is why this method exists
private User createUser(String email, String username, String password, String publicname, String provider) throws IOException {
    UserApi userApi = privilegedGitLabApi.getUserApi();
    if (password == null) {
        // generate a random password
        password = RandomStringUtils.random(12, 0, GitlabUtils.PWD_CHARS.length - 1, false, false, GitlabUtils.PWD_CHARS, new SecureRandom());
    }
    User user = new User();
    user.setEmail(email);
    user.setUsername(username);
    user.setName(publicname);
    user.setIsAdmin(false);
    user.setSkipConfirmation(true);
    if (provider != null) {
        Identity identity = new Identity();
        identity.setExternUid(username);
        identity.setProvider(provider);
        user.setIdentities(Collections.singletonList(identity));
    }
    try {
        // do not send a pwd reset link
        user = userApi.createUser(user, password, false);
        return user;
    } catch (GitLabApiException e) {
        throw new IOException("Failed to create user", e);
    }
}
Also used : User(org.gitlab4j.api.models.User) GitUser(de.catma.repository.git.GitUser) SecureRandom(java.security.SecureRandom) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) UserApi(org.gitlab4j.api.UserApi) Identity(org.gitlab4j.api.models.Identity)

Example 2 with Identity

use of org.gitlab4j.api.models.Identity in project OpenUnison by TremoloSecurity.

the class GitlabUserProvider method findUser.

@Override
public User findUser(String userID, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    org.gitlab4j.api.models.User fromGitlab = findUserByName(userID);
    if (fromGitlab == null) {
        return null;
    }
    User forUnison = new User(userID);
    for (String attrName : attributes) {
        try {
            String val = beanUtils.getProperty(fromGitlab, attrName);
            if (val != null) {
                Attribute attr = new Attribute(attrName, val);
                forUnison.getAttribs().put(attrName, attr);
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new ProvisioningException("Couldn't load attribute " + attrName, e);
        }
    }
    if (fromGitlab.getIdentities() != null) {
        ArrayList<GitlabFedIdentity> ids = new ArrayList<GitlabFedIdentity>();
        for (Identity fedid : fromGitlab.getIdentities()) {
            GitlabFedIdentity id = new GitlabFedIdentity();
            id.setExternalUid(fedid.getExternUid());
            id.setProvider(fedid.getProvider());
            ids.add(id);
        }
        request.put(GitlabUserProvider.GITLAB_IDENTITIES, ids);
    }
    ArrayList<Header> defheaders = new ArrayList<Header>();
    defheaders.add(new BasicHeader("Private-Token", this.token));
    BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager(cfgMgr.getHttpClientSocketRegistry());
    RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false).build();
    CloseableHttpClient http = HttpClients.custom().setConnectionManager(bhcm).setDefaultHeaders(defheaders).setDefaultRequestConfig(rc).build();
    try {
        HttpGet getmembers = new HttpGet(new StringBuilder().append(this.url).append("/api/v4/users/").append(fromGitlab.getId()).append("/memberships").toString());
        CloseableHttpResponse resp = http.execute(getmembers);
        if (resp.getStatusLine().getStatusCode() != 200) {
            throw new IOException("Invalid response " + resp.getStatusLine().getStatusCode());
        }
        String json = EntityUtils.toString(resp.getEntity());
        JSONArray members = (JSONArray) new JSONParser().parse(json);
        for (Object o : members) {
            JSONObject member = (JSONObject) o;
            String sourceType = (String) member.get("source_type");
            String sourceName = (String) member.get("source_name");
            if (sourceType.equalsIgnoreCase("Namespace")) {
                forUnison.getGroups().add(sourceName);
            }
        }
    } catch (IOException | ParseException e) {
        throw new ProvisioningException("Could not get group memebers", e);
    } finally {
        try {
            http.close();
        } catch (IOException e) {
        }
        bhcm.close();
    }
    return forUnison;
}
Also used : User(com.tremolosecurity.provisioning.core.User) Attribute(com.tremolosecurity.saml.Attribute) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Identity(org.gitlab4j.api.models.Identity) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

IOException (java.io.IOException)2 Identity (org.gitlab4j.api.models.Identity)2 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)1 User (com.tremolosecurity.provisioning.core.User)1 Attribute (com.tremolosecurity.saml.Attribute)1 GitUser (de.catma.repository.git.GitUser)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SecureRandom (java.security.SecureRandom)1 ArrayList (java.util.ArrayList)1 Header (org.apache.http.Header)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 BasicHttpClientConnectionManager (org.apache.http.impl.conn.BasicHttpClientConnectionManager)1 BasicHeader (org.apache.http.message.BasicHeader)1 GitLabApiException (org.gitlab4j.api.GitLabApiException)1 UserApi (org.gitlab4j.api.UserApi)1 User (org.gitlab4j.api.models.User)1 JSONArray (org.json.simple.JSONArray)1