/*
 * Copyright 2015 Open mHealth
 *
 * 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 org.openmhealth.shim;


import org.springframework.data.domain.Sort;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.client.token.ClientTokenServices;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.util.SerializationUtils;

import java.util.List;

/**
 * @author Danilo Bonilla
 */
public class AccessParameterClientTokenServices implements ClientTokenServices {

    private AccessParametersRepo accessParametersRepo;

    public AccessParameterClientTokenServices(AccessParametersRepo accessParametersRepo) {
        this.accessParametersRepo = accessParametersRepo;
    }

    @Override
    public OAuth2AccessToken getAccessToken(OAuth2ProtectedResourceDetails resource,
                                            Authentication authentication) {
        String username = authentication.getPrincipal().toString();
        String shimKey = authentication.getDetails().toString();

        AccessParameters accessParameters = accessParametersRepo.findByUsernameAndShimKey(
            username, shimKey, new Sort(Sort.Direction.DESC, "dateCreated"));

        if (accessParameters == null || accessParameters.getSerializedToken() == null) {
            return null; //No token was found!
        }

        return SerializationUtils.deserialize(accessParameters.getSerializedToken());
    }

    @Override
    public void saveAccessToken(OAuth2ProtectedResourceDetails resource,
                                Authentication authentication, OAuth2AccessToken accessToken) {
        String username = authentication.getPrincipal().toString();
        String shimKey = authentication.getDetails().toString();
        AccessParameters accessParameters = accessParametersRepo.findByUsernameAndShimKey(
            username, shimKey, new Sort(Sort.Direction.DESC, "dateCreated"));

        if (accessParameters == null) {
            accessParameters = new AccessParameters();
            accessParameters.setUsername(username);
            accessParameters.setShimKey(shimKey);
        }

        accessParameters.setSerializedToken(SerializationUtils.serialize(accessToken));
        accessParametersRepo.save(accessParameters);
    }

    @Override
    public void removeAccessToken(OAuth2ProtectedResourceDetails resource, Authentication authentication) {
        String username = authentication.getPrincipal().toString();
        String shimKey = authentication.getDetails().toString();

        List<AccessParameters> accessParameters =
            accessParametersRepo.findAllByUsernameAndShimKey(username, shimKey);
        for (AccessParameters accessParameter : accessParameters) {
            accessParametersRepo.delete(accessParameter);
        }
    }
}

@@ -16,9 +16,9 @@
  201
-5
+7
  Ope
@@ -1061,16 +1061,17 @@
 .List;%0A%0A
+%0A
 /**%0A * @
@@ -1321,24 +1321,25 @@
 tersRepo) %7B%0A
+%0A
         this
@@ -1441,32 +1441,45 @@
  getAccessToken(
+%0A            
 OAuth2ProtectedR
@@ -1507,40 +1507,8 @@
 ce,%0A
-                                
     
@@ -1540,32 +1540,33 @@
 thentication) %7B%0A
+%0A
         String u
@@ -1778,32 +1778,36 @@
 ey(%0A            
+    
 username, shimKe
@@ -2135,32 +2135,45 @@
 saveAccessToken(
+%0A            
 OAuth2ProtectedR
@@ -2197,36 +2197,16 @@
 source,%0A
-                    
         
@@ -2269,24 +2269,25 @@
 essToken) %7B%0A
+%0A
         Stri
@@ -2399,32 +2399,33 @@
 s().toString();%0A
+%0A
         AccessPa
@@ -2443,32 +2443,48 @@
 cessParameters =
+%0A               
  accessParameter
@@ -2519,32 +2519,44 @@
 ey(%0A            
+     
+       
 username, shimKe
@@ -2544,33 +2544,81 @@
    username,
- shimKey,
+%0A                        shimKey,%0A                       
  new Sort(So
@@ -2946,24 +2946,25 @@
 essToken));%0A
+%0A
         acce
@@ -3129,24 +3129,25 @@
 tication) %7B%0A
+%0A
         Stri
@@ -3317,28 +3317,16 @@
 meters =
-%0A           
  accessP
@@ -3391,80 +3391,9 @@
 y);%0A
-        for (AccessParameters accessParameter : accessParameters) %7B%0A    
+%0A
     
@@ -3439,29 +3439,20 @@
 arameter
+s
 );%0A
-        %7D%0A
     %7D%0A%7D%0A

Clean up code