1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.amber.oauth2.client.demo.controller;
23
24 import java.io.IOException;
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.apache.amber.oauth2.client.URLConnectionClient;
28 import org.apache.amber.oauth2.client.demo.Utils;
29 import org.apache.amber.oauth2.client.demo.model.OAuthParams;
30 import org.apache.amber.oauth2.client.demo.model.OAuthRegParams;
31 import org.apache.amber.oauth2.client.request.OAuthClientRequest;
32 import org.apache.amber.oauth2.common.exception.OAuthProblemException;
33 import org.apache.amber.oauth2.common.exception.OAuthSystemException;
34 import org.apache.amber.oauth2.ext.dynamicreg.client.OAuthRegistrationClient;
35 import org.apache.amber.oauth2.ext.dynamicreg.client.request.OAuthClientRegistrationRequest;
36 import org.apache.amber.oauth2.ext.dynamicreg.common.OAuthRegistration;
37 import org.apache.amber.oauth2.ext.dynamicreg.client.response.OAuthClientRegistrationResponse;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.ModelAttribute;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.servlet.ModelAndView;
42
43 import org.apache.amber.oauth2.client.demo.exception.ApplicationException;
44
45
46
47
48
49
50 @Controller
51 @RequestMapping("/")
52 public class RegistrationController {
53
54 @RequestMapping(value = "/register")
55 public ModelAndView authorize(@ModelAttribute("oauthRegParams") OAuthRegParams oauthRegParams,
56 @ModelAttribute("oauthParams") OAuthParams oauthParams,
57 HttpServletRequest req) throws OAuthSystemException, IOException {
58
59
60 try {
61
62 Utils.validateRegistrationParams(oauthRegParams);
63
64 OAuthClientRequest request = null;
65 if (Utils.REG_TYPE_PULL.equals(oauthRegParams.getRegistrationType())) {
66 request = OAuthClientRegistrationRequest
67 .location(oauthRegParams.getRegistrationEndpoint(), OAuthRegistration.Type.PULL)
68 .setUrl(oauthRegParams.getUrl())
69 .buildBodyMessage();
70 } else {
71 request = OAuthClientRegistrationRequest
72 .location(oauthRegParams.getRegistrationEndpoint(), OAuthRegistration.Type.PUSH)
73 .setName(oauthRegParams.getName())
74 .setUrl(oauthRegParams.getUrl())
75 .setDescription(oauthRegParams.getDescription())
76 .setRedirectURL(oauthRegParams.getRedirectUri())
77 .setIcon(oauthRegParams.getIcon())
78 .buildBodyMessage();
79 }
80
81
82 OAuthRegistrationClient client = new OAuthRegistrationClient(new URLConnectionClient());
83 OAuthClientRegistrationResponse response = client.clientInfo(request);
84
85 oauthParams.setClientId(response.getClientId());
86 oauthParams.setClientSecret(response.getClientSecret());
87 oauthParams.setAuthzEndpoint(oauthRegParams.getAuthzEndpoint());
88 oauthParams.setTokenEndpoint(oauthRegParams.getTokenEndpoint());
89 oauthParams.setApplication(oauthRegParams.getApplication());
90 oauthParams.setRedirectUri(oauthRegParams.getRedirectUri());
91
92 return new ModelAndView("get_authz");
93
94
95 } catch (ApplicationException e) {
96 oauthRegParams.setErrorMessage(e.getMessage());
97 return new ModelAndView("register");
98 } catch (OAuthProblemException e) {
99 oauthRegParams.setErrorMessage(e.getMessage());
100 return new ModelAndView("register");
101 }
102 }
103
104 }