8
8
import org .elasticsearch .Version ;
9
9
import org .elasticsearch .action .ActionRequest ;
10
10
import org .elasticsearch .action .ActionRequestValidationException ;
11
+ import org .elasticsearch .common .CharArrays ;
11
12
import org .elasticsearch .common .Nullable ;
12
13
import org .elasticsearch .common .Strings ;
13
14
import org .elasticsearch .common .bytes .BytesArray ;
14
15
import org .elasticsearch .common .bytes .BytesReference ;
15
16
import org .elasticsearch .common .io .stream .StreamInput ;
16
17
import org .elasticsearch .common .io .stream .StreamOutput ;
17
18
import org .elasticsearch .common .settings .SecureString ;
18
- import org .elasticsearch .common .CharArrays ;
19
19
20
20
import java .io .IOException ;
21
21
import java .util .Arrays ;
22
22
import java .util .Collections ;
23
23
import java .util .EnumSet ;
24
+ import java .util .Locale ;
24
25
import java .util .Set ;
25
26
import java .util .stream .Collectors ;
26
27
@@ -35,6 +36,7 @@ public final class CreateTokenRequest extends ActionRequest {
35
36
36
37
public enum GrantType {
37
38
PASSWORD ("password" ),
39
+ KERBEROS ("_kerberos" ),
38
40
REFRESH_TOKEN ("refresh_token" ),
39
41
AUTHORIZATION_CODE ("authorization_code" ),
40
42
CLIENT_CREDENTIALS ("client_credentials" );
@@ -62,21 +64,23 @@ public static GrantType fromString(String grantType) {
62
64
}
63
65
64
66
private static final Set <GrantType > SUPPORTED_GRANT_TYPES = Collections .unmodifiableSet (
65
- EnumSet .of (GrantType .PASSWORD , GrantType .REFRESH_TOKEN , GrantType .CLIENT_CREDENTIALS ));
67
+ EnumSet .of (GrantType .PASSWORD , GrantType .KERBEROS , GrantType . REFRESH_TOKEN , GrantType .CLIENT_CREDENTIALS ));
66
68
67
69
private String grantType ;
68
70
private String username ;
69
71
private SecureString password ;
72
+ private SecureString kerberosTicket ;
70
73
private String scope ;
71
74
private String refreshToken ;
72
75
73
76
public CreateTokenRequest () {}
74
77
75
- public CreateTokenRequest (String grantType , @ Nullable String username , @ Nullable SecureString password , @ Nullable String scope ,
76
- @ Nullable String refreshToken ) {
78
+ public CreateTokenRequest (String grantType , @ Nullable String username , @ Nullable SecureString password ,
79
+ @ Nullable SecureString kerberosTicket , @ Nullable String scope , @ Nullable String refreshToken ) {
77
80
this .grantType = grantType ;
78
81
this .username = username ;
79
82
this .password = password ;
83
+ this .kerberosTicket = kerberosTicket ;
80
84
this .scope = scope ;
81
85
this .refreshToken = refreshToken ;
82
86
}
@@ -88,43 +92,28 @@ public ActionRequestValidationException validate() {
88
92
if (type != null ) {
89
93
switch (type ) {
90
94
case PASSWORD :
91
- if ( Strings . isNullOrEmpty ( username )) {
92
- validationException = addValidationError ( "username is missing" , validationException );
93
- }
94
- if ( password == null || password . getChars () == null || password . getChars (). length == 0 ) {
95
- validationException = addValidationError ( "password is missing" , validationException ) ;
96
- }
97
- if ( refreshToken != null ) {
98
- validationException =
99
- addValidationError ( "refresh_token is not supported with the password grant_type" , validationException );
100
- }
95
+ validationException = validateUnsupportedField ( type , "kerberos_ticket" , kerberosTicket , validationException );
96
+ validationException = validateUnsupportedField ( type , "refresh_token" , refreshToken , validationException );
97
+ validationException = validateRequiredField ( "username" , username , validationException );
98
+ validationException = validateRequiredField ( "password" , password , validationException );
99
+ break ;
100
+ case KERBEROS :
101
+ validationException = validateUnsupportedField ( type , "username" , username , validationException );
102
+ validationException = validateUnsupportedField ( type , "password" , password , validationException );
103
+ validationException = validateUnsupportedField ( type , "refresh_token" , refreshToken , validationException );
104
+ validationException = validateRequiredField ( "kerberos_ticket" , kerberosTicket , validationException );
101
105
break ;
102
106
case REFRESH_TOKEN :
103
- if (username != null ) {
104
- validationException =
105
- addValidationError ("username is not supported with the refresh_token grant_type" , validationException );
106
- }
107
- if (password != null ) {
108
- validationException =
109
- addValidationError ("password is not supported with the refresh_token grant_type" , validationException );
110
- }
111
- if (refreshToken == null ) {
112
- validationException = addValidationError ("refresh_token is missing" , validationException );
113
- }
107
+ validationException = validateUnsupportedField (type , "username" , username , validationException );
108
+ validationException = validateUnsupportedField (type , "password" , password , validationException );
109
+ validationException = validateUnsupportedField (type , "kerberos_ticket" , kerberosTicket , validationException );
110
+ validationException = validateRequiredField ("refresh_token" , refreshToken , validationException );
114
111
break ;
115
112
case CLIENT_CREDENTIALS :
116
- if (username != null ) {
117
- validationException =
118
- addValidationError ("username is not supported with the client_credentials grant_type" , validationException );
119
- }
120
- if (password != null ) {
121
- validationException =
122
- addValidationError ("password is not supported with the client_credentials grant_type" , validationException );
123
- }
124
- if (refreshToken != null ) {
125
- validationException = addValidationError ("refresh_token is not supported with the client_credentials grant_type" ,
126
- validationException );
127
- }
113
+ validationException = validateUnsupportedField (type , "username" , username , validationException );
114
+ validationException = validateUnsupportedField (type , "password" , password , validationException );
115
+ validationException = validateUnsupportedField (type , "kerberos_ticket" , kerberosTicket , validationException );
116
+ validationException = validateUnsupportedField (type , "refresh_token" , refreshToken , validationException );
128
117
break ;
129
118
default :
130
119
validationException = addValidationError ("grant_type only supports the values: [" +
@@ -139,6 +128,32 @@ public ActionRequestValidationException validate() {
139
128
return validationException ;
140
129
}
141
130
131
+ private static ActionRequestValidationException validateRequiredField (String field , String fieldValue ,
132
+ ActionRequestValidationException validationException ) {
133
+ if (Strings .isNullOrEmpty (fieldValue )) {
134
+ validationException = addValidationError (String .format (Locale .ROOT , "%s is missing" , field ), validationException );
135
+ }
136
+ return validationException ;
137
+ }
138
+
139
+ private static ActionRequestValidationException validateRequiredField (String field , SecureString fieldValue ,
140
+ ActionRequestValidationException validationException ) {
141
+ if (fieldValue == null || fieldValue .getChars () == null || fieldValue .length () == 0 ) {
142
+ validationException = addValidationError (String .format (Locale .ROOT , "%s is missing" , field ), validationException );
143
+ }
144
+ return validationException ;
145
+ }
146
+
147
+ private static ActionRequestValidationException validateUnsupportedField (GrantType grantType , String field , Object fieldValue ,
148
+ ActionRequestValidationException validationException ) {
149
+ if (fieldValue != null ) {
150
+ validationException = addValidationError (
151
+ String .format (Locale .ROOT , "%s is not supported with the %s grant_type" , field , grantType .getValue ()),
152
+ validationException );
153
+ }
154
+ return validationException ;
155
+ }
156
+
142
157
public void setGrantType (String grantType ) {
143
158
this .grantType = grantType ;
144
159
}
@@ -151,6 +166,10 @@ public void setPassword(@Nullable SecureString password) {
151
166
this .password = password ;
152
167
}
153
168
169
+ public void setKerberosTicket (@ Nullable SecureString kerberosTicket ) {
170
+ this .kerberosTicket = kerberosTicket ;
171
+ }
172
+
154
173
public void setScope (@ Nullable String scope ) {
155
174
this .scope = scope ;
156
175
}
@@ -173,6 +192,11 @@ public SecureString getPassword() {
173
192
return password ;
174
193
}
175
194
195
+ @ Nullable
196
+ public SecureString getKerberosTicket () {
197
+ return kerberosTicket ;
198
+ }
199
+
176
200
@ Nullable
177
201
public String getScope () {
178
202
return scope ;
0 commit comments