15
15
import jakarta .ejb .Stateless ;
16
16
import jakarta .inject .Inject ;
17
17
import jakarta .inject .Named ;
18
+ import jakarta .ws .rs .WebApplicationException ;
18
19
import jakarta .ws .rs .core .Response ;
19
20
import org .slf4j .Logger ;
20
21
21
22
import java .util .Arrays ;
22
23
import java .util .List ;
23
24
import java .util .stream .Collectors ;
24
25
26
+ /**
27
+ * Provides methods to validate different params about SSA.
28
+ */
25
29
@ Named
26
30
@ Stateless
27
31
public class SsaRestWebServiceValidator {
@@ -38,7 +42,14 @@ public class SsaRestWebServiceValidator {
38
42
@ Inject
39
43
private ScopeService scopeService ;
40
44
41
- public Client getClientFromSession () {
45
+ /**
46
+ * Get client from session
47
+ *
48
+ * @return {@link Client} if obtained.
49
+ * @throws WebApplicationException with status {@code 401} and key <b>INVALID_CLIENT</b> if the client cannot
50
+ * be obtained.
51
+ */
52
+ public Client getClientFromSession () throws WebApplicationException {
42
53
SessionClient sessionClient = identity .getSessionClient ();
43
54
if (sessionClient != null ) {
44
55
log .debug ("Client: {}, obtained from session" , sessionClient .getClient ().getClientId ());
@@ -47,13 +58,27 @@ public Client getClientFromSession() {
47
58
throw errorResponseFactory .createBadRequestException (SsaErrorResponseType .INVALID_CLIENT , "Invalid client" );
48
59
}
49
60
50
- public void checkScopesPolicy (Client client , String scope ) {
61
+ /**
62
+ * Check if the client has the given scope.
63
+ *
64
+ * @param client Client to check scope
65
+ * @param scope Scope to validate
66
+ * @throws WebApplicationException with status {@code 401} and key <b>UNAUTHORIZED_CLIENT</b> if you don't have the scope.
67
+ */
68
+ public void checkScopesPolicy (Client client , String scope ) throws WebApplicationException {
51
69
List <String > scopes = scopeService .getScopeIdsByDns (Arrays .stream (client .getScopes ()).collect (Collectors .toList ()));
52
70
if (!scopes .contains (scope ))
53
71
throw errorResponseFactory .createWebApplicationException (Response .Status .UNAUTHORIZED , SsaErrorResponseType .UNAUTHORIZED_CLIENT , "Unauthorized client" );
54
72
}
55
73
56
- public void checkScopesPolicy (Client client , List <String > scopeList ) {
74
+ /**
75
+ * Check if the client has at least one scope from the list of scopes.
76
+ *
77
+ * @param client Client to check scope
78
+ * @param scopeList List of scope to validated
79
+ * @throws WebApplicationException with status {@code 401} and key <b>UNAUTHORIZED_CLIENT</b> if you don't have the scope.
80
+ */
81
+ public void checkScopesPolicy (Client client , List <String > scopeList ) throws WebApplicationException {
57
82
if (client == null || scopeList == null || scopeList .isEmpty ()) {
58
83
throw errorResponseFactory .createWebApplicationException (Response .Status .UNAUTHORIZED , SsaErrorResponseType .UNAUTHORIZED_CLIENT , "Unauthorized client" );
59
84
}
0 commit comments