/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.decathlon.gravitee.extension.policy;

import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;
import io.gravitee.policy.api.PolicyChain;
import io.gravitee.policy.api.PolicyResult;
import io.gravitee.policy.api.annotations.OnRequest;
import io.gravitee.policy.api.annotations.OnResponse;
import io.jsonwebtoken.Jwts;

@SuppressWarnings("unused")
public class JWTPolicy {

    /**
     * The associated configuration to this JWT Policy
     */
    private JWTPolicyConfiguration configuration;

    /**
     * Create a new JWT Policy instance based on its associated configuration
     *
     * @param configuration the associated configuration to the new JWT Policy instance
     */
    public JWTPolicy(JWTPolicyConfiguration configuration) {
        this.configuration = configuration;
    }

    @OnRequest
    public void onRequest(Request request, Response response, PolicyChain policyChain) {
        // Add a dummy header
        //request.headers().set("X-DummyHeader", configuration.getStringParam());
        String jwt = request.headers().get("Authorization").get(0);

        try {
            String iss = (String) Jwts.parser().parse(jwt).getHeader().get("iss");
        } catch (Exception e) {

        }

        // Finally continue chaining
        policyChain.doNext(request, response);
    }

//    @OnResponse
//    public void onResponse(Request request, Response response, PolicyChain policyChain) {
//        if (isASuccessfulResponse(response)) {
//            policyChain.doNext(request, response);
//        } else {
//            policyChain.failWith(new PolicyResult() {
//                @Override
//                public boolean isFailure() {
//                    return true;
//                }
//
//                @Override
//                public int httpStatusCode() {
//                    return HttpStatusCode.INTERNAL_SERVER_ERROR_500;
//                }
//
//                @Override
//                public String message() {
//                    return "Not a successful response :-(";
//                }
//            });
//        }
//    }
//
//    private static boolean isASuccessfulResponse(Response response) {
//        switch (response.status() / 100) {
//            case 1:
//            case 2:
//            case 3:
//                return true;
//            default:
//                return false;
//        }
//    }

}

@@ -674,16 +674,60 @@
 olicy;%0A%0A
+import io.gravitee.common.http.HttpHeaders;%0A
 import i
@@ -1663,126 +1663,154 @@
     
-// Add a dummy header%0A        //request.headers().set(%22X-DummyHeader%22, configuration.getStringParam());%0A        String
+try %7B%0A            String jwt = request.headers().getFirst(HttpHeaders.AUTHORIZATION).split(%22 %22)%5B1%5D;%0A            if (jwt == null) %7B%0A               
  jwt
@@ -1812,36 +1812,39 @@
   jwt = request.
-head
+paramet
 ers().get(%22Autho
@@ -1842,32 +1842,23 @@
 et(%22
-Authorization%22).get(0);%0A
+access_token%22);
 %0A   
@@ -1854,37 +1854,38 @@
 oken%22);%0A        
-try %7B
+    %7D%0A
 %0A            Str
@@ -1877,24 +1877,30 @@
             
+final 
 String iss =
@@ -1954,24 +1954,205 @@
 get(%22iss%22);%0A
+            final String key = configuration.getPublicKey(iss);%0A            Jwts.parser().setSigningKey(key).parseClaimsJwt(jwt);%0A            policyChain.doNext(request, response);%0A
         %7D ca

add gitignore