Search in sources :

Example 1 with JwtToken

use of io.jenkins.blueocean.auth.jwt.JwtToken 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)

Aggregations

Plugin (hudson.Plugin)1 User (hudson.model.User)1 Mailer (hudson.tasks.Mailer)1 JwtToken (io.jenkins.blueocean.auth.jwt.JwtToken)1 JSONObject (net.sf.json.JSONObject)1 Authentication (org.acegisecurity.Authentication)1