OAuth2AuthorizationCodeIssuer.java
7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.oauth.jwt.JwsBuilder
* com.adobe.granite.oauth.jwt.JwsBuilderFactory
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.oltu.oauth2.as.response.OAuthASResponse
* org.apache.oltu.oauth2.as.response.OAuthASResponse$OAuthAuthorizationResponseBuilder
* org.apache.oltu.oauth2.common.exception.OAuthProblemException
* org.apache.oltu.oauth2.common.exception.OAuthSystemException
* org.apache.oltu.oauth2.common.message.OAuthResponse
* org.apache.oltu.oauth2.common.message.OAuthResponse$OAuthErrorResponseBuilder
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.oauth.server.impl;
import com.adobe.granite.oauth.jwt.JwsBuilder;
import com.adobe.granite.oauth.jwt.JwsBuilderFactory;
import com.adobe.granite.oauth.server.OAuth2ResourceServer;
import com.adobe.granite.oauth.server.impl.OAuth2GraniteIssuer;
import com.adobe.granite.oauth.server.impl.helper.OAuth2Helper;
import java.io.IOException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/granite/oauth/authorize"})})
public class OAuth2AuthorizationCodeIssuer
extends SlingAllMethodsServlet {
@Reference
private JwsBuilderFactory jwsBuilderFactory;
private static final long serialVersionUID = -6280552132723613511L;
private final Logger logger;
@Reference
private SlingRepository repository;
@Reference
private OAuth2ResourceServer oAuth2ResourceServer;
public OAuth2AuthorizationCodeIssuer() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Session oauthServiceSession = null;
try {
String authorized = request.getParameter("accept");
String redirectUri = request.getParameter("redirect_uri").replace(":__", "://");
if (authorized == null) {
OAuthProblemException oAuthProblemException = OAuthProblemException.error((String)"access_denied", (String)"Access denied");
oAuthProblemException.setRedirectUri(redirectUri);
throw oAuthProblemException;
}
String scope = request.getParameter("scope");
oauthServiceSession = this.repository.loginService(null, null);
OAuth2Helper.validateScopes(this.oAuth2ResourceServer, OAuth2Helper.getScopesSet(scope), false);
String clientId = request.getParameter("client_id");
OAuth2Helper.validateAuthorizationEndpointInput(oauthServiceSession, clientId, redirectUri);
OAuth2GraniteIssuer oauthIssuer = new OAuth2GraniteIssuer(this.jwsBuilderFactory.getInstance("HS256")).setScope(scope).setAudience(clientId).setSubject(request.getRemoteUser()).setExpiresIn("600");
oauthIssuer.setCustomClaimsSetField("cty", "code");
String authorizationCode = oauthIssuer.authorizationCode();
OAuthResponse resp = OAuthASResponse.authorizationResponse((HttpServletRequest)request, (int)302).location(redirectUri).setCode(authorizationCode).buildQueryMessage();
response.sendRedirect(resp.getLocationUri());
}
catch (OAuthSystemException e) {
OAuth2Helper.handleOAuthSystemException(e, (HttpServletResponse)response);
}
catch (OAuthProblemException e) {
this.logger.error("doPost: OAuth Problem Exception in the Authorization Endpoint", (Throwable)e);
try {
String redirectUri = e.getRedirectUri();
if (redirectUri == null) {
response.sendError(e.getResponseStatus(), e.getError());
} else {
OAuthResponse resp = OAuthASResponse.errorResponse((int)302).error(e).location(e.getRedirectUri()).buildQueryMessage();
response.sendRedirect(resp.getLocationUri());
}
}
catch (OAuthSystemException e1) {
OAuth2Helper.handleOAuthSystemException(e1, (HttpServletResponse)response);
}
}
catch (RepositoryException e) {
OAuth2Helper.handleOAuthSystemException(new OAuthSystemException("failed while accessing repository"), (HttpServletResponse)response);
}
finally {
if (oauthServiceSession != null) {
oauthServiceSession.logout();
}
}
}
protected void bindJwsBuilderFactory(JwsBuilderFactory jwsBuilderFactory) {
this.jwsBuilderFactory = jwsBuilderFactory;
}
protected void unbindJwsBuilderFactory(JwsBuilderFactory jwsBuilderFactory) {
if (this.jwsBuilderFactory == jwsBuilderFactory) {
this.jwsBuilderFactory = null;
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindOAuth2ResourceServer(OAuth2ResourceServer oAuth2ResourceServer) {
this.oAuth2ResourceServer = oAuth2ResourceServer;
}
protected void unbindOAuth2ResourceServer(OAuth2ResourceServer oAuth2ResourceServer) {
if (this.oAuth2ResourceServer == oAuth2ResourceServer) {
this.oAuth2ResourceServer = null;
}
}
}