001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nelson Silva <nsilva@nuxeo.com>
018 */
019package org.nuxeo.ecm.restapi.server.jaxrs.login.tokenauth;
020
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.DocumentModelList;
023import org.nuxeo.ecm.core.api.NuxeoPrincipal;
024import org.nuxeo.ecm.tokenauth.io.AuthenticationToken;
025import org.nuxeo.ecm.webengine.model.WebObject;
026import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
027import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
028import org.nuxeo.ecm.tokenauth.service.TokenAuthenticationService;
029import org.nuxeo.runtime.api.Framework;
030
031import javax.ws.rs.DELETE;
032import javax.ws.rs.GET;
033import javax.ws.rs.POST;
034import javax.ws.rs.Path;
035import javax.ws.rs.PathParam;
036import javax.ws.rs.Produces;
037import javax.ws.rs.QueryParam;
038import javax.ws.rs.core.MediaType;
039import javax.ws.rs.core.Response;
040import java.util.Calendar;
041import java.util.List;
042import java.util.Map;
043import java.util.stream.Collectors;
044
045/**
046 * Token Object
047 *
048 * @since 8.3
049 */
050@WebObject(type = "token")
051@Produces(MediaType.APPLICATION_JSON)
052public class AuthenticationTokensObject extends AbstractResource<ResourceTypeImpl> {
053
054    private TokenAuthenticationService service;
055
056    @Override
057    protected void initialize(Object... args) {
058        service = Framework.getService(TokenAuthenticationService.class);
059    }
060
061    @GET
062    public List<AuthenticationToken> getTokens(@QueryParam("application") String applicationName) {
063        DocumentModelList tokens = service.getTokenBindings(getCurrentUser().getName(), applicationName);
064        return tokens.stream().map(this::asAuthenticationToken).collect(Collectors.toList());
065    }
066
067    @POST
068    public Response createToken(@QueryParam("application") String applicationName,
069            @QueryParam("deviceId") String deviceId, @QueryParam("deviceDescription") String deviceDescription,
070            @QueryParam("permission") String permission) {
071        String username = getCurrentUser().getName();
072        String token = service.acquireToken(username, applicationName, deviceId, deviceDescription, permission);
073        return Response.ok(token).status(Response.Status.CREATED).build();
074    }
075
076    @DELETE
077    @Path("{token}")
078    public void deleteToken(@PathParam("token") String tokenId) {
079        if (tokenId == null) {
080            return;
081        }
082        service.revokeToken(tokenId);
083    }
084
085    private NuxeoPrincipal getCurrentUser() {
086        return getContext().getCoreSession().getPrincipal();
087    }
088
089    private AuthenticationToken asAuthenticationToken(DocumentModel entry) {
090        Map<String, Object> props = entry.getProperties("authtoken");
091        AuthenticationToken token = new AuthenticationToken(
092                (String) props.get("token"),
093                (String) props.get("userName"),
094                (String) props.get("applicationName"),
095                (String) props.get("deviceId"),
096                (String) props.get("deviceDescription"),
097                (String) props.get("permission"));
098        token.setCreationDate((Calendar) props.get("creationDate"));
099        return token;
100    }
101}