Search in sources :

Example 1 with JSONObject

use of net.sf.json.JSONObject in project head by mifos.

the class DatabaseConfiguration method readVCAPConfiguration.

private void readVCAPConfiguration() throws ConfigurationException {
    final String vcapServicesVar = System.getenv(VCAP_SERVICES_VAR);
    if (vcapServicesVar != null) {
        // use database configuration from the system variable to replace the default config
        final JSONObject json = (JSONObject) JSONSerializer.toJSON(vcapServicesVar);
        String mysqlKey = null;
        @SuppressWarnings("rawtypes") final Iterator iterator = json.keys();
        while (iterator.hasNext()) {
            final String key = (String) iterator.next();
            if (key.startsWith("mysql")) {
                mysqlKey = key;
                break;
            }
        }
        if (mysqlKey == null) {
            throw new ConfigurationException(INVALID_STRUCTURE);
        }
        final JSON mysqlJson = (JSON) json.get(mysqlKey);
        JSONObject dbJson;
        if (mysqlJson.isArray()) {
            final JSONArray mysqlJsonArray = (JSONArray) mysqlJson;
            if (mysqlJsonArray.size() < 1) {
                throw new ConfigurationException(INVALID_STRUCTURE);
            }
            dbJson = (JSONObject) mysqlJsonArray.get(0);
        } else {
            dbJson = (JSONObject) mysqlJson;
        }
        final JSONObject credentialsJson = (JSONObject) dbJson.get("credentials");
        this.dbName = credentialsJson.getString("name");
        this.host = credentialsJson.getString("host");
        this.port = credentialsJson.getString("port");
        this.user = credentialsJson.getString("user");
        this.password = credentialsJson.getString("password");
        this.dbPentahoDW = credentialsJson.getString("dbPentahoDW");
    }
}
Also used : JSONObject(net.sf.json.JSONObject) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) Iterator(java.util.Iterator) JSONArray(net.sf.json.JSONArray) JSON(net.sf.json.JSON)

Example 2 with JSONObject

use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.

the class ModelObjectSerializerTest method test_json.

@Test
public void test_json() throws IOException {
    String xJson = ModelObjectSerializer.toJson(new X());
    JSONObject jsonObj = JSONObject.fromObject(xJson);
    Assert.assertEquals(X.class.getName(), jsonObj.getString("_class"));
    Assert.assertEquals("xVal", jsonObj.getString("val"));
}
Also used : JSONObject(net.sf.json.JSONObject) Test(org.junit.Test)

Example 3 with JSONObject

use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.

the class JwtImpl method getToken.

@Override
public JwtToken getToken(@Nullable @QueryParameter("expiryTimeInMins") Integer expiryTimeInMins, @Nullable @QueryParameter("maxExpiryTimeInMins") Integer maxExpiryTimeInMins) {
    String t = System.getProperty("EXPIRY_TIME_IN_MINS");
    long expiryTime = DEFAULT_EXPIRY_IN_SEC;
    if (t != null) {
        expiryTime = Integer.parseInt(t);
    }
    int maxExpiryTime = DEFAULT_MAX_EXPIRY_TIME_IN_MIN;
    t = System.getProperty("MAX_EXPIRY_TIME_IN_MINS");
    if (t != null) {
        maxExpiryTime = Integer.parseInt(t);
    }
    if (maxExpiryTimeInMins != null) {
        maxExpiryTime = maxExpiryTimeInMins;
    }
    if (expiryTimeInMins != null) {
        if (expiryTimeInMins > maxExpiryTime) {
            throw new ServiceException.BadRequestExpception(String.format("expiryTimeInMins %s can't be greated than %s", expiryTimeInMins, maxExpiryTime));
        }
        expiryTime = expiryTimeInMins * 60;
    }
    Authentication authentication = Jenkins.getInstance().getAuthentication();
    if (authentication == null) {
        throw new ServiceException.UnauthorizedException("Unauthorized: No login session found");
    }
    String userId = authentication.getName();
    User user = User.get(userId, false, Collections.emptyMap());
    String email = null;
    String fullName = null;
    if (user != null) {
        fullName = user.getFullName();
        userId = user.getId();
        Mailer.UserProperty p = user.getProperty(Mailer.UserProperty.class);
        if (p != null)
            email = p.getAddress();
    }
    Plugin plugin = Jenkins.getInstance().getPlugin("blueocean-jwt");
    String issuer = "blueocean-jwt:" + ((plugin != null) ? plugin.getWrapper().getVersion() : "");
    JwtToken jwtToken = new JwtToken();
    jwtToken.claim.put("jti", UUID.randomUUID().toString().replace("-", ""));
    jwtToken.claim.put("iss", issuer);
    jwtToken.claim.put("sub", userId);
    jwtToken.claim.put("name", fullName);
    long currentTime = System.currentTimeMillis() / 1000;
    jwtToken.claim.put("iat", currentTime);
    jwtToken.claim.put("exp", currentTime + expiryTime);
    jwtToken.claim.put("nbf", currentTime - DEFAULT_NOT_BEFORE_IN_SEC);
    //set claim
    JSONObject context = new JSONObject();
    JSONObject userObject = new JSONObject();
    userObject.put("id", userId);
    userObject.put("fullName", fullName);
    userObject.put("email", email);
    context.put("user", userObject);
    jwtToken.claim.put("context", context);
    return jwtToken;
}
Also used : JwtToken(io.jenkins.blueocean.auth.jwt.JwtToken) User(hudson.model.User) JSONObject(net.sf.json.JSONObject) Authentication(org.acegisecurity.Authentication) Mailer(hudson.tasks.Mailer) Plugin(hudson.Plugin)

Example 4 with JSONObject

use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.

the class JwtImplTest method anonymousUserToken.

@Test
public void anonymousUserToken() throws Exception {
    j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
    JenkinsRule.WebClient webClient = j.createWebClient();
    Page page = webClient.goTo("jwt-auth/token/", null);
    String token = page.getWebResponse().getResponseHeaderValue("X-BLUEOCEAN-JWT");
    Assert.assertNotNull(token);
    JsonWebStructure jsonWebStructure = JsonWebStructure.fromCompactSerialization(token);
    Assert.assertTrue(jsonWebStructure instanceof JsonWebSignature);
    JsonWebSignature jsw = (JsonWebSignature) jsonWebStructure;
    String kid = jsw.getHeader("kid");
    Assert.assertNotNull(kid);
    page = webClient.goTo("jwt-auth/jwks/" + kid + "/", "application/json");
    //        for(NameValuePair valuePair: page.getWebResponse().getResponseHeaders()){
    //            System.out.println(valuePair);
    //        }
    JSONObject jsonObject = JSONObject.fromObject(page.getWebResponse().getContentAsString());
    RsaJsonWebKey rsaJsonWebKey = new RsaJsonWebKey(jsonObject, null);
    JwtConsumer jwtConsumer = new JwtConsumerBuilder().setRequireExpirationTime().setAllowedClockSkewInSeconds(// allow some leeway in validating time based claims to account for clock skew
    30).setRequireSubject().setVerificationKey(// verify the sign with the public key
    rsaJsonWebKey.getKey()).build();
    JwtClaims claims = jwtConsumer.processToClaims(token);
    Assert.assertEquals("anonymous", claims.getSubject());
    Map<String, Object> claimMap = claims.getClaimsMap();
    Map<String, Object> context = (Map<String, Object>) claimMap.get("context");
    Map<String, String> userContext = (Map<String, String>) context.get("user");
    Assert.assertEquals("anonymous", userContext.get("id"));
}
Also used : JwtClaims(org.jose4j.jwt.JwtClaims) JwtConsumerBuilder(org.jose4j.jwt.consumer.JwtConsumerBuilder) Page(com.gargoylesoftware.htmlunit.Page) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) JsonWebSignature(org.jose4j.jws.JsonWebSignature) JSONObject(net.sf.json.JSONObject) JwtConsumer(org.jose4j.jwt.consumer.JwtConsumer) JSONObject(net.sf.json.JSONObject) RsaJsonWebKey(org.jose4j.jwk.RsaJsonWebKey) Map(java.util.Map) JsonWebStructure(org.jose4j.jwx.JsonWebStructure) Test(org.junit.Test)

Example 5 with JSONObject

use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.

the class BluePipeline method update.

/**
     * Updates this pipeline using {@link BluePipelineUpdateRequest}
     * @param staplerRequest stapler request
     * @return Updated BluePipeline instance
     * @throws IOException throws IOException in certain cases
     */
@PUT
@WebMethod(name = "")
@TreeResponse
public BluePipeline update(StaplerRequest staplerRequest) throws IOException {
    JSONObject body = JSONObject.fromObject(IOUtils.toString(staplerRequest.getReader()));
    if (body.get("$class") == null) {
        throw new ServiceException.BadRequestExpception("$class is required element");
    }
    BluePipelineUpdateRequest request = staplerRequest.bindJSON(BluePipelineUpdateRequest.class, body);
    return update(request);
}
Also used : JSONObject(net.sf.json.JSONObject) WebMethod(org.kohsuke.stapler.WebMethod) TreeResponse(io.jenkins.blueocean.commons.stapler.TreeResponse) PUT(org.kohsuke.stapler.verb.PUT)

Aggregations

JSONObject (net.sf.json.JSONObject)522 Test (org.junit.Test)100 JSONArray (net.sf.json.JSONArray)98 IOException (java.io.IOException)55 HashMap (java.util.HashMap)54 ArrayList (java.util.ArrayList)39 JSON (net.sf.json.JSON)27 Map (java.util.Map)26 PrintWriter (java.io.PrintWriter)25 File (java.io.File)23 ApiException (org.zaproxy.zap.extension.api.ApiException)20 InputStream (java.io.InputStream)18 URISyntaxException (java.net.URISyntaxException)16 JSONException (net.sf.json.JSONException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 URI (java.net.URI)14 JsonConfig (net.sf.json.JsonConfig)14 Context (org.zaproxy.zap.model.Context)14 FreeStyleBuild (hudson.model.FreeStyleBuild)13 URL (java.net.URL)13