<commit_before>package org.gluu.oxauth.session.ws.rs;

import org.apache.commons.lang.StringUtils;
import org.gluu.oxauth.model.common.User;
import org.gluu.oxauth.model.configuration.AppConfiguration;
import org.gluu.oxauth.model.registration.Client;
import org.gluu.oxauth.model.token.JsonWebResponse;
import org.gluu.oxauth.model.token.JwrService;
import org.gluu.oxauth.service.SectorIdentifierService;
import org.json.JSONObject;
import org.msgpack.core.Preconditions;
import org.slf4j.Logger;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * @author <NAME>
 */
@Stateless
@Named
public class LogoutTokenFactory {

    private static final String EVENTS_KEY = "http://schemas.openid.net/event/backchannel-logout";

    @Inject
    private Logger log;

    @Inject
    private AppConfiguration appConfiguration;

    @Inject
    private JwrService jwrService;

    @Inject
    private SectorIdentifierService sectorIdentifierService;

    public JsonWebResponse createLogoutToken(Client client, User user, String sessionId) {
        try {
            Preconditions.checkNotNull(client);

            JsonWebResponse jwr = jwrService.createJwr(client);

            fillClaims(jwr, client, user, sessionId);

            jwrService.encode(jwr, client);
            return jwr;
        } catch (Exception e) {
            log.error("Failed to create logout_token for client:" + client.getClientId());
            return null;
        }
    }

    private void fillClaims(JsonWebResponse jwr, Client client, User user, String sessionId) {
        int lifeTime = appConfiguration.getIdTokenLifetime();
        Calendar calendar = Calendar.getInstance();
        Date issuedAt = calendar.getTime();
        calendar.add(Calendar.SECOND, lifeTime);
        Date expiration = calendar.getTime();

        jwr.getClaims().setExpirationTime(expiration);
        jwr.getClaims().setIssuedAt(issuedAt);
        jwr.getClaims().setIssuer(appConfiguration.getIssuer());
        jwr.getClaims().setAudience(client.getClientId());
        jwr.getClaims().setJwtId(UUID.randomUUID());
        jwr.getClaims().setClaim("events", getLogoutTokenEvents());

        if (StringUtils.isNotBlank(sessionId)) {
            jwr.getClaims().setClaim("sid", sessionId);
        }

        final String sub = sectorIdentifierService.getSub(client, user);
        if (StringUtils.isNotBlank(sub)) {
            jwr.getClaims().setSubjectIdentifier(sub);
        }
    }

    private JSONObject getLogoutTokenEvents() {
        final JSONObject events = new JSONObject();<commit_msg>included sid claim into logout_token only if backchannel_logout_session_required=true #70<commit_after>package org.gluu.oxauth.session.ws.rs;

import org.apache.commons.lang.StringUtils;
import org.gluu.oxauth.model.common.User;
import org.gluu.oxauth.model.configuration.AppConfiguration;
import org.gluu.oxauth.model.registration.Client;
import org.gluu.oxauth.model.token.JsonWebResponse;
import org.gluu.oxauth.model.token.JwrService;
import org.gluu.oxauth.service.SectorIdentifierService;
import org.json.JSONObject;
import org.msgpack.core.Preconditions;
import org.slf4j.Logger;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * @author <NAME>
 */
@Stateless
@Named
public class LogoutTokenFactory {

    private static final String EVENTS_KEY = "http://schemas.openid.net/event/backchannel-logout";

    @Inject
    private Logger log;

    @Inject
    private AppConfiguration appConfiguration;

    @Inject
    private JwrService jwrService;

    @Inject
    private SectorIdentifierService sectorIdentifierService;

    public JsonWebResponse createLogoutToken(Client client, User user, String sessionId) {
        try {
            Preconditions.checkNotNull(client);

            JsonWebResponse jwr = jwrService.createJwr(client);

            fillClaims(jwr, client, user, sessionId);

            jwrService.encode(jwr, client);
            return jwr;
        } catch (Exception e) {
            log.error("Failed to create logout_token for client:" + client.getClientId());
            return null;
        }
    }

    private void fillClaims(JsonWebResponse jwr, Client client, User user, String sessionId) {
        int lifeTime = appConfiguration.getIdTokenLifetime();
        Calendar calendar = Calendar.getInstance();
        Date issuedAt = calendar.getTime();
        calendar.add(Calendar.SECOND, lifeTime);
        Date expiration = calendar.getTime();

        jwr.getClaims().setExpirationTime(expiration);
        jwr.getClaims().setIssuedAt(issuedAt);
        jwr.getClaims().setIssuer(appConfiguration.getIssuer());
        jwr.getClaims().setAudience(client.getClientId());
        jwr.getClaims().setJwtId(UUID.randomUUID());
        jwr.getClaims().setClaim("events", getLogoutTokenEvents());

        if (StringUtils.isNotBlank(sessionId) && client.getAttributes().getBackchannelLogoutSessionRequired()) {
            jwr.getClaims().setClaim("sid", sessionId);
        }

        final String sub = sectorIdentifierService.getSub(client, user);
        if (StringUtils.isNotBlank(sub)) {
            jwr.getClaims().setSubjectIdentifier(sub);
        }
    }

    private JSONObject getLogoutTokenEvents() {
        final JSONObject events = new JSONObject();
{'fraction_non_alphanumeric': 0.10641399416909621, 'fraction_numerical': 0.0007288629737609329, 'mean_word_length': 3.4230459307010475, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5031345', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1590, 'n_words': 302}