TODO
[facebook-errors] / facebook / FacebookUtils.java
1 package de.juplo.facebook;
2
3
4 import de.juplo.facebook.token.SignedRequestAwareAuthorizationCodeAccessTokenProvider;
5 import de.juplo.facebook.client.GraphApiErrorHandler;
6 import java.util.Arrays;
7 import java.util.LinkedList;
8 import java.util.List;
9 import javax.annotation.PostConstruct;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.beans.BeansException;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.beans.factory.config.BeanPostProcessor;
15 import org.springframework.context.annotation.Bean;
16 import org.springframework.context.annotation.Configuration;
17 import org.springframework.security.oauth2.client.OAuth2RestTemplate;
18 import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler;
19 import org.springframework.security.oauth2.client.token.AccessTokenProvider;
20 import org.springframework.security.oauth2.client.token.AccessTokenProviderChain;
21 import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider;
22 import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider;
23 import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider;
24
25
26
27 /**
28  * This class injects the facebook-utils into the spring-oauth2-configuration
29  *
30  * @author kai
31  */
32 @Configuration
33 public class FacebookUtils
34 {
35   private final Logger log = LoggerFactory.getLogger(FacebookUtils.class);
36
37
38   @Autowired(required=false)
39   private List<AccessTokenProvider> accessTokenProviderChain;
40
41
42   @PostConstruct
43   public void init()
44   {
45     if (accessTokenProviderChain == null)
46     {
47       log.info("no AccessTokenProviderChain configured, creating default-chain");
48       accessTokenProviderChain =
49           Arrays.<AccessTokenProvider> asList(
50               new ImplicitAccessTokenProvider(),
51               new ResourceOwnerPasswordAccessTokenProvider(),
52               new ClientCredentialsAccessTokenProvider()
53               );
54     }
55   }
56
57
58   @Bean
59   public BeanPostProcessor getBeanPostProcessor(final String clientSecret)
60   {
61     log.debug("createing new instance of BeanPostProcessor");
62     return new BeanPostProcessor() {
63
64       @Override
65       public Object postProcessBeforeInitialization(
66           Object bean,
67           String beanName
68           )
69           throws
70             BeansException
71       {
72         if (bean instanceof OAuth2RestTemplate)
73         {
74           log.info("injecting signed_request-aware AccessTokenProviderChain");
75           OAuth2RestTemplate template = (OAuth2RestTemplate)bean;
76           List<AccessTokenProvider> chain =
77               new LinkedList<>(accessTokenProviderChain);
78           SignedRequestAwareAuthorizationCodeAccessTokenProvider provider =
79               new SignedRequestAwareAuthorizationCodeAccessTokenProvider();
80           provider.setSecret(clientSecret);
81           chain.add(provider);
82           template.setAccessTokenProvider(new AccessTokenProviderChain(chain));
83           log.info("injecting GraphApiErrorHandler");
84           template.setErrorHandler(
85               new GraphApiErrorHandler(
86                   (OAuth2ErrorHandler)template.getErrorHandler()
87                   )
88               );
89         }
90
91         return bean;
92       }
93
94       @Override
95       public Object postProcessAfterInitialization(
96           Object bean,
97           String beanName
98           )
99           throws
100             BeansException
101       {
102         return bean;
103       }
104     };
105   }
106 }