11package org .springframework .security .oauth2 .provider ;
22
3+ import java .io .Serializable ;
4+ import java .util .Collection ;
5+ import java .util .Collections ;
6+ import java .util .HashMap ;
7+ import java .util .HashSet ;
38import java .util .Map ;
49import java .util .Set ;
510
11+ import org .springframework .security .core .GrantedAuthority ;
612import org .springframework .security .oauth2 .common .util .OAuth2Utils ;
713
814/**
9- * Client token for a request for a authorization.
15+ * Base class representing a request for authorization. There are convenience methods for the well-known properties
16+ * required by the OAUth2 spec, and a set of generic parameters to allow for extensions.
1017 *
1118 * @author Ryan Heaton
1219 * @author Dave Syer
1320 */
14- public class AuthorizationRequest extends ClientToken {
21+ public class AuthorizationRequest implements Serializable {
1522
16- private final String state ;
23+ private static final String CLIENT_ID = "client_id" ;
1724
18- private final String requestedRedirect ;
25+ private static final String CLIENT_SECRET = "client_secret" ;
1926
20- private boolean denied ;
27+ private static final String STATE = "state" ;
2128
22- public AuthorizationRequest (String clientId , String clientSecret , Set <String > scope , String state ,
29+ private static final String SCOPE = "scope" ;
30+
31+ private static final String REDIRECT_URI = "redirect_uri" ;
32+
33+ private final Set <String > scope ;
34+
35+ private final Set <String > resourceIds ;
36+
37+ private final boolean denied ;
38+
39+ private final Collection <GrantedAuthority > authorities ;
40+
41+ private final Map <String , String > parameters = new HashMap <String , String >();
42+
43+ public AuthorizationRequest (Map <String , String > parameters ) {
44+ this (parameters .get (CLIENT_ID ), null , OAuth2Utils .parseScope (parameters .get ("scope" )), null , null , false ,
45+ parameters .get (STATE ), parameters .get (REDIRECT_URI ));
46+ this .parameters .putAll (parameters );
47+ }
48+
49+ public AuthorizationRequest (String clientId , String clientSecret , Collection <String > scope ,
50+ Collection <GrantedAuthority > authorities , Collection <String > resourceIds ) {
51+ this (clientId , clientSecret , scope , authorities , resourceIds , false , null , null );
52+ }
53+
54+ private AuthorizationRequest (AuthorizationRequest copy , boolean denied ) {
55+ this (copy .getClientId (), copy .getClientSecret (), copy .scope , copy .authorities , copy .resourceIds , denied , copy
56+ .getState (), copy .getRequestedRedirect ());
57+ this .parameters .putAll (copy .parameters );
58+ }
59+
60+ private AuthorizationRequest (String clientId , String clientSecret , Collection <String > scope ,
61+ Collection <GrantedAuthority > authorities , Collection <String > resourceIds , boolean denied , String state ,
2362String requestedRedirect ) {
24- super (clientId , clientSecret , scope );
25- this .state = state ;
26- this .requestedRedirect = requestedRedirect ;
63+ this .resourceIds = resourceIds == null ? null : Collections .unmodifiableSet (new HashSet <String >(resourceIds ));
64+ this .scope = scope == null ? Collections .<String > emptySet () : Collections .unmodifiableSet (new HashSet <String >(
65+ scope ));
66+ this .authorities = authorities == null ? null : new HashSet <GrantedAuthority >(authorities );
67+ this .denied = denied ;
68+ parameters .put (CLIENT_ID , clientId );
69+ parameters .put (CLIENT_SECRET , clientSecret );
70+ parameters .put (STATE , state );
71+ parameters .put (REDIRECT_URI , requestedRedirect );
72+ parameters .put (SCOPE , OAuth2Utils .formatScope (scope ));
2773}
2874
29- public AuthorizationRequest (Map <String , String > parameters ) {
30- this (parameters .get ("client_id" ), null , OAuth2Utils .parseScope (parameters .get ("scope" )), parameters
31- .get ("state" ), parameters .get ("redirect_uri" ));
75+ public Map <String , String > getParameters () {
76+ return Collections .unmodifiableMap (parameters );
3277}
3378
34- public String getRequestedRedirect () {
35- return requestedRedirect ;
79+ public String getClientId () {
80+ return parameters . get ( CLIENT_ID ) ;
3681}
3782
38- public String getState () {
39- return state ;
83+ public String getClientSecret () {
84+ return parameters .get (CLIENT_SECRET );
85+ }
86+
87+ public Set <String > getScope () {
88+ return this .scope ;
89+ }
90+
91+ public Set <String > getResourceIds () {
92+ return resourceIds ;
93+ }
94+
95+ public Collection <GrantedAuthority > getAuthorities () {
96+ return authorities ;
97+ }
98+
99+ public boolean isAuthenticated () {
100+ return !denied ;
40101}
41102
42103public boolean isDenied () {
43104return denied ;
44105}
45106
46- // TODO: make this immutable
47- public void setDenied (boolean denied ) {
48- this .denied = denied ;
49- setApproved (!denied );
107+ public AuthorizationRequest denied (boolean denied ) {
108+ return new AuthorizationRequest (this , denied );
109+ }
110+
111+ public String getState () {
112+ return parameters .get (STATE );
113+ }
114+
115+ public String getRequestedRedirect () {
116+ return parameters .get (REDIRECT_URI );
117+ }
118+
119+ @ Override
120+ public int hashCode () {
121+ final int prime = 31 ;
122+ int result = 1 ;
123+ result = prime * result + ((authorities == null ) ? 0 : authorities .hashCode ());
124+ result = prime * result + (denied ? 1231 : 1237 );
125+ result = prime * result + ((parameters == null ) ? 0 : parameters .hashCode ());
126+ result = prime * result + ((resourceIds == null ) ? 0 : resourceIds .hashCode ());
127+ result = prime * result + ((scope == null ) ? 0 : scope .hashCode ());
128+ return result ;
129+ }
130+
131+ @ Override
132+ public boolean equals (Object obj ) {
133+ if (this == obj )
134+ return true ;
135+ if (obj == null )
136+ return false ;
137+ if (getClass () != obj .getClass ())
138+ return false ;
139+ AuthorizationRequest other = (AuthorizationRequest ) obj ;
140+ if (authorities == null ) {
141+ if (other .authorities != null )
142+ return false ;
143+ }
144+ else if (!authorities .equals (other .authorities ))
145+ return false ;
146+ if (denied != other .denied )
147+ return false ;
148+ if (parameters == null ) {
149+ if (other .parameters != null )
150+ return false ;
151+ }
152+ else if (!parameters .equals (other .parameters ))
153+ return false ;
154+ if (resourceIds == null ) {
155+ if (other .resourceIds != null )
156+ return false ;
157+ }
158+ else if (!resourceIds .equals (other .resourceIds ))
159+ return false ;
160+ if (scope == null ) {
161+ if (other .scope != null )
162+ return false ;
163+ }
164+ else if (!scope .equals (other .scope ))
165+ return false ;
166+ return true ;
50167}
51168
52169}
0 commit comments