001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Portions copyright 2011-2013 The MITRE Corporation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *******************************************************************************/
018package org.mitre.openid.connect.web;
019
020import java.util.Map;
021
022import org.mitre.jwt.signer.service.JWTSigningAndValidationService;
023import org.mitre.openid.connect.view.JWKSetView;
024import org.springframework.beans.factory.annotation.Autowired;
025import org.springframework.http.MediaType;
026import org.springframework.stereotype.Controller;
027import org.springframework.ui.Model;
028import org.springframework.web.bind.annotation.RequestMapping;
029
030import com.nimbusds.jose.jwk.JWK;
031
032@Controller
033public class JWKSetPublishingEndpoint {
034
035        public static final String URL = "jwk";
036
037        @Autowired
038        private JWTSigningAndValidationService jwtService;
039
040        @RequestMapping(value = "/" + URL, produces = MediaType.APPLICATION_JSON_VALUE)
041        public String getJwk(Model m) {
042
043                // map from key id to key
044                Map<String, JWK> keys = jwtService.getAllPublicKeys();
045
046                // TODO: check if keys are empty, return a 404 here or just an empty list?
047
048                m.addAttribute("keys", keys);
049
050                return JWKSetView.VIEWNAME;
051        }
052
053        /**
054         * @return the jwtService
055         */
056        public JWTSigningAndValidationService getJwtService() {
057                return jwtService;
058        }
059
060        /**
061         * @param jwtService the jwtService to set
062         */
063        public void setJwtService(JWTSigningAndValidationService jwtService) {
064                this.jwtService = jwtService;
065        }
066
067}