74 if (representation == null)
75 throw new ErrorResponseException(OAuthErrorException.INVALID_REQUEST,
"invalid_permission", Response.Status.BAD_REQUEST);
76 if (representation.getId() != null)
77 throw new ErrorResponseException(
"invalid_permission",
"created permissions should not have id", Response.Status.BAD_REQUEST);
78 if (representation.getResource() == null)
79 throw new ErrorResponseException(
"invalid_permission",
"created permissions should have resource", Response.Status.BAD_REQUEST);
80 if (representation.getScope() == null && representation.getScopeName() == null)
81 throw new ErrorResponseException(
"invalid_permission",
"created permissions should have scope or scopeName", Response.Status.BAD_REQUEST);
82 if (representation.getRequester() == null && representation.getRequesterName() == null)
83 throw new ErrorResponseException(
"invalid_permission",
"created permissions should have requester or requesterName", Response.Status.BAD_REQUEST);
87 if (resource == null )
throw new ErrorResponseException(
"invalid_resource_id",
"Resource set with id [" + representation.getResource() +
"] does not exists in this server.", Response.Status.BAD_REQUEST);
90 throw new ErrorResponseException(
"not_authorised",
"permissions for [" + representation.getResource() +
"] can be only created by the owner", Response.Status.FORBIDDEN);
92 UserModel user = null;
93 if(representation.getRequester() != null)
99 throw new ErrorResponseException(
"invalid_permission",
"Requester does not exists in this server as user.", Response.Status.BAD_REQUEST);
104 if(representation.getScopeName() != null)
109 if (scope == null && representation.getScope() !=null )
110 throw new ErrorResponseException(
"invalid_scope",
"Scope [" + representation.getScope() +
"] is invalid", Response.Status.BAD_REQUEST);
111 if (scope == null && representation.getScopeName() !=null )
112 throw new ErrorResponseException(
"invalid_scope",
"Scope [" + representation.getScopeName() +
"] is invalid", Response.Status.BAD_REQUEST);
114 boolean match = resource.getScopes().contains(scope);
117 throw new ErrorResponseException(
"invalid_resource_id",
"Resource set with id [" + representation.getResource() +
"] does not have Scope [" + scope.getName() +
"]", Response.Status.BAD_REQUEST);
119 Map<String, String> attributes =
new HashMap<String, String>();
120 attributes.put(PermissionTicket.RESOURCE, resource.getId());
121 attributes.put(PermissionTicket.SCOPE, scope.getId());
122 attributes.put(PermissionTicket.REQUESTER, user.getId());
125 throw new ErrorResponseException(
"invalid_permission",
"Permission already exists", Response.Status.BAD_REQUEST);
127 PermissionTicket ticket = ticketStore.create(resource.getId(), scope.getId(), user.getId(),
resourceServer);
128 if(representation.isGranted())
129 ticket.setGrantedTimestamp(java.lang.System.currentTimeMillis());
130 representation = ModelToRepresentation.toRepresentation(ticket,
authorization);
131 return Response.ok(representation).build();
final ResourceServer resourceServer
Definition: PermissionTicketService.java:61
final AuthorizationProvider authorization
Definition: PermissionTicketService.java:59
PermissionTicketStore getPermissionTicketStore()
Scope findByName(String name, String resourceServerId)
final KeycloakIdentity identity
Definition: PermissionTicketService.java:60
ScopeStore getScopeStore()
RealmModel getRealm()
Definition: AuthorizationProvider.java:169
StoreFactory getStoreFactory()
Definition: AuthorizationProvider.java:109
KeycloakSession getKeycloakSession()
Definition: AuthorizationProvider.java:165
Resource findById(String id, String resourceServerId)
UserProvider userStorageManager()
String getId()
Definition: KeycloakIdentity.java:214
ResourceStore getResourceStore()
UserModel getUserById(String id, RealmModel realm)
UserModel getUserByUsername(String username, RealmModel realm)