1919import com .iexec .common .result .ResultModel ;
2020import com .iexec .commons .poco .chain .ChainDeal ;
2121import com .iexec .commons .poco .chain .ChainTask ;
22- import com .iexec .commons .poco .chain .ChainTaskStatus ;
2322import com .iexec .commons .poco .chain .WorkerpoolAuthorization ;
2423import com .iexec .commons .poco .security .Signature ;
2524import com .iexec .commons .poco .tee .TeeUtils ;
3231import org .springframework .dao .DataAccessException ;
3332import org .springframework .stereotype .Service ;
3433
34+ import java .time .Instant ;
3535import java .util .Optional ;
3636
3737import static com .iexec .resultproxy .authorization .AuthorizationError .*;
@@ -50,8 +50,19 @@ public AuthorizationService(AuthorizationRepository authorizationRepository, Iex
5050
5151 /**
5252 * Checks whether this execution is authorized.
53- * If not authorized, return the reason.
54- * Otherwise, returns an empty {@link Optional}.
53+ * <p>
54+ * The following conditions have to be verified:
55+ * <ul>
56+ * <li>The {@code WorkerpoolAuthorization} must not be null and must contain a task ID
57+ * <li>The task must be retrieved from the blockchain network
58+ * <li>The current timestamp must be before the task final deadline
59+ * <li>The deal must be retrieved from the blockchain network
60+ * <li>If the {@code WorkerpoolAuthorization} contains an enclave challenge, the on-chain deal must have a correct tag
61+ * <li>The {@code WorkerpoolAuthorization} has been signed with the private key of the workerpool owner found in the deal
62+ * </ul>
63+ *
64+ * @param workerpoolAuthorization The authorization to check
65+ * @return the reason if unauthorized, an empty {@code Optional} otherwise
5566 */
5667 public Optional <AuthorizationError > isAuthorizedOnExecutionWithDetailedIssue (WorkerpoolAuthorization workerpoolAuthorization ) {
5768 if (workerpoolAuthorization == null || StringUtils .isEmpty (workerpoolAuthorization .getChainTaskId ())) {
@@ -60,38 +71,36 @@ public Optional<AuthorizationError> isAuthorizedOnExecutionWithDetailedIssue(Wor
6071 }
6172
6273 final String chainTaskId = workerpoolAuthorization .getChainTaskId ();
63- Optional < ChainTask > optionalChainTask = iexecHubService .getChainTask (chainTaskId );
64- if (optionalChainTask . isEmpty () ) {
74+ final ChainTask chainTask = iexecHubService .getChainTask (chainTaskId ). orElse ( null );
75+ if (chainTask == null ) {
6576 log .error ("Could not get chainTask [chainTaskId:{}]" , chainTaskId );
6677 return Optional .of (GET_CHAIN_TASK_FAILED );
6778 }
68- final ChainTask chainTask = optionalChainTask .get ();
6979
70- final ChainTaskStatus taskStatus = chainTask .getStatus ();
71- if (taskStatus != ChainTaskStatus . ACTIVE ) {
72- log .error ("Task not active onchain [chainTaskId:{}, status :{}]" ,
73- chainTaskId , taskStatus );
74- return Optional .of (TASK_NOT_ACTIVE );
80+ final long deadline = chainTask .getFinalDeadline ();
81+ if (Instant . now (). isAfter ( Instant . ofEpochMilli ( deadline )) ) {
82+ log .error ("Task deadline reached [chainTaskId:{}, deadline :{}]" ,
83+ chainTaskId , Instant . ofEpochMilli ( deadline ) );
84+ return Optional .of (TASK_FINAL_DEADLINE_REACHED );
7585 }
7686
7787 final String chainDealId = chainTask .getDealid ();
78- Optional < ChainDeal > optionalChainDeal = iexecHubService .getChainDeal (chainDealId );
79- if (optionalChainDeal . isEmpty () ) {
88+ final ChainDeal chainDeal = iexecHubService .getChainDeal (chainDealId ). orElse ( null );
89+ if (chainDeal == null ) {
8090 log .error ("isAuthorizedOnExecution failed (getChainDeal failed) [chainTaskId:{}]" , chainTaskId );
8191 return Optional .of (GET_CHAIN_DEAL_FAILED );
8292 }
83- ChainDeal chainDeal = optionalChainDeal .get ();
8493
85- final boolean isTeeTask = !workerpoolAuthorization .getEnclaveChallenge ().equals (BytesUtils .EMPTY_HEX_STRING_32 );
94+ final boolean isTeeTask = !workerpoolAuthorization .getEnclaveChallenge ().equals (BytesUtils .EMPTY_ADDRESS );
8695 final boolean isTeeTaskOnchain = TeeUtils .isTeeTag (chainDeal .getTag ());
8796 if (isTeeTask != isTeeTaskOnchain ) {
88- log .error ("Could not match onchain task type [isTeeTask:{}, isTeeTaskOnchain:{}, chainTaskId:{}, walletAddress:{}]" ,
97+ log .error ("Could not match on-chain task type [isTeeTask:{}, isTeeTaskOnchain:{}, chainTaskId:{}, walletAddress:{}]" ,
8998 isTeeTask , isTeeTaskOnchain , chainTaskId , workerpoolAuthorization .getWorkerWallet ());
9099 return Optional .of (NO_MATCH_ONCHAIN_TYPE );
91100 }
92101
93- String workerpoolAddress = chainDeal .getPoolOwner ();
94- boolean isSignedByWorkerpool = isSignedByHimself (workerpoolAuthorization .getHash (),
102+ final String workerpoolAddress = chainDeal .getPoolOwner ();
103+ final boolean isSignedByWorkerpool = isSignedByHimself (workerpoolAuthorization .getHash (),
95104 workerpoolAuthorization .getSignature ().getValue (), workerpoolAddress );
96105
97106 if (!isSignedByWorkerpool ) {
0 commit comments