# oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
# Copyright (c) 2017, Gluu
#
# Author: Jose Gonzalez
# Adapted from previous 3.0.1 script of Yuriy Movchan
#
# oxConfigurationProperty required: 
#   allowed_clients - comma separated list of dns of allowed clients 
#   (i.e. the SCIM RP client)

from org.xdi.model.custom.script.type.uma import UmaRptPolicyType
from org.xdi.service.cdi.util import CdiUtil
from org.xdi.oxauth.security import Identity
from org.xdi.model.uma import ClaimDefinitionBuilder
from org.xdi.util import StringHelper, ArrayHelper
from java.util import Arrays, ArrayList, HashSet
from java.lang import String

class UmaRptPolicy(UmaRptPolicyType):

    def __init__(self, currentTimeMillis):
        self.currentTimeMillis = currentTimeMillis

    def init(self, configurationAttributes):
        print "RPT Policy. Initializing ..."
        self.clientsSet = self.prepareClientsSet(configurationAttributes)
        print "RPT Policy. Initialized successfully"
        return True

    def destroy(self, configurationAttributes):
        print "RPT Policy. Destroyed successfully"
        return True

    def getApiVersion(self):
        return 1

    def getRequiredClaims(self, context): 
        json = """[
        ]"""
        return ClaimDefinitionBuilder.build(json)
        
    def authorize(self, context): # context is reference of org.xdi.oxauth.uma.authorization.UmaAuthorizationContext
        print "RPT Policy. Authorizing ..."
        
        identity = CdiUtil.bean(Identity)
        client_id = identity.getSessionClient().getClient().getClientId()
        
        print "UmaRptPolicy. client_id=", client_id

        if (StringHelper.isEmpty(client_id)):
            return False
     
        if (self.clientsSet.contains(client_id)):
            print "UmaRptPolicy. Authorizing client"
            return True
        else:
            print "UmaRptPolicy. Client isn't authorized"
            return False

        
    def prepareClientsSet(self, configurationAttributes):
        clientsSet = HashSet()
        if (not configurationAttributes.containsKey("allowed_clients")):
            return clientsSet

        allowedClientsList = configurationAttributes.get("allowed_clients").getValue2()
        if (StringHelper.isEmpty(allowedClientsList)):
            print "UmaRptPolicy. The property allowed_clients is empty"
            return clientsSet    

        allowedClientsListArray = StringHelper.split(allowedClientsList, ",")
        if (ArrayHelper.isEmpty(allowedClientsListArray)):
            print "UmaRptPolicy. No clients specified in allowed_clients property"
            return clientsSet
        
        # Convert to HashSet to quick search
        i = 0
        count = len(allowedClientsListArray)
        while (i < count):
            client = allowedClientsListArray[i]
            clientsSet.add(client)
            i = i + 1

        return clientsSet

@@ -44,10 +44,10 @@
  (20
-08
+17
 ). S
@@ -243,17 +243,16 @@
 equired:
- 
 %0A#   all
@@ -312,17 +312,16 @@
  clients
- 
 %0A#   (i.
@@ -459,53 +459,8 @@
 til%0A
-from org.xdi.oxauth.security import Identity%0A
 from
@@ -1218,17 +1218,16 @@
 ontext):
- 
 %0A       
@@ -1302,25 +1302,17 @@
 d(json)%0A
-        
 %0A
+
     def 
@@ -1468,106 +1468,34 @@
 ..%22%0A
-        %0A        identity = CdiUtil.bean(Identity)%0A        client_id = identity.getSessionClient()
+%0A        client_id=context
 .get
@@ -1517,25 +1517,16 @@
 entId()%0A
-        %0A
         
@@ -1559,11 +1559,16 @@
 t_id
-=%22,
+ = %25s%22 %25
  cli

Fix for https://github.com/GluuFederation/oxAuth/issues/692