 /****************************************************************************
 The MIT License (MIT)

 Copyright (c) 2016 Apigee Corporation

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/
 var apiProducts = JSON.parse(context.getVariable('apiProducts')).ApiProducts.ApiProduct || [];
 
 var apiProductsList = [];
 try {
     //get only the product name; status is not used/sent
     apiProducts.forEach(function(apiProduct){
        apiProductsList.push(apiProduct.Name); 
     });
 }catch(err){
     apiProductsList.push(apiProducts.Name);
 }
 
 var scope = [];
 try {
    context.setVariable("scope",context.getVariable("oauthv2accesstoken.AccessTokenRequest.scope").split(" ") || []);
 } catch (err) {
     scope = [];
 }
 
 context.setVariable("apiProductList", apiProductsList);
 context.setVariable("iss", context.getVariable("proxyProto") + "://" + context.getVariable("proxyHost") + context.getVariable("proxy.basepath")+context.getVariable("proxy.pathsuffix"));
 context.setVariable("jti", 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
                return v.toString(16);
            }));
 context.setVariable("developerEMail", context.getVariable("apigee.developer.email"));
 context.setVariable("developerApp", context.getVariable("apigee.developer.app.name"));

 

// Copyright 2018 Google LLC
//
// 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.
var apiProducts = JSON.parse(context.getVariable('apiProducts'));

//{"ApiProducts":{"ApiProduct":{"Name":"helloworld","Status":"approved"}}}
//{"ApiProducts":{"ApiProduct":[{"Name":"httpbin product","Status":"approved"},{"Name":"Edgemicro hello","Status":"approved"}]}}
var apiProductsList = [];
try {

    if (!Array.isArray(apiProducts)) {
        if (Array.isArray(apiProducts.ApiProducts.ApiProduct)) { // hack for broken XML2JSON
            apiProducts.ApiProducts.ApiProduct.forEach(function(apiProduct) {
                apiProductsList.push(apiProduct.Name);
            });
        } else {
            apiProductsList.push(apiProducts.ApiProducts.ApiProduct.Name);
        }
    } else {
        //get only the product name; status is not used/sent
        apiProducts.forEach(function(apiProduct) {
            apiProductsList.push(apiProduct.Name);
        });

    }
} catch (err) {
    if (apiProducts && apiProducts.Name) {
        apiProductsList.push(apiProducts.Name);
    }
}

var scope = context.getVariable("oauthv2accesstoken.AccessTokenRequest.scope");
if (scope) {
    var scopearr = scope.split(" ");
    context.setVariable("scope", scopearr.join());
}

context.setVariable("apiProductList", apiProductsList.join());
context.setVariable("iss", context.getVariable("proxyProto") + "://" + context.getVariable("proxyHost") + context.getVariable("proxy.basepath") + context.getVariable("proxy.pathsuffix"));
context.setVariable("jti", 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random() * 16 | 0,
        v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
}));

handle multiple products and keys