forked from yegor256/takes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPsChain.java
More file actions
67 lines (60 loc) · 1.57 KB
/
Copy pathPsChain.java
File metadata and controls
67 lines (60 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* SPDX-FileCopyrightText: Copyright (c) 2014-2026 Yegor Bugayenko
* SPDX-License-Identifier: MIT
*/
package org.takes.facets.auth;
import lombok.EqualsAndHashCode;
import org.cactoos.list.ListOf;
import org.takes.Request;
import org.takes.Response;
import org.takes.misc.Opt;
/**
* Chain of passes that attempts authentication through multiple mechanisms.
* This implementation tries each pass in sequence until one succeeds,
* providing a fallback mechanism for authentication.
*
* <p>The class is immutable and thread-safe.
*
* @since 0.1
*/
@EqualsAndHashCode
public final class PsChain implements Pass {
/**
* Collection of passes to attempt in sequence.
*/
private final Iterable<Pass> passes;
/**
* Ctor.
* @param list Passes
*/
public PsChain(final Pass... list) {
this(new ListOf<>(list));
}
/**
* Ctor.
* @param list Passes
*/
public PsChain(final Iterable<Pass> list) {
this.passes = list;
}
@Override
public Opt<Identity> enter(final Request req) throws Exception {
Opt<Identity> user = new Opt.Empty<>();
for (final Pass pass : this.passes) {
user = pass.enter(req);
if (user.has()) {
break;
}
}
return user;
}
@Override
public Response exit(final Response response,
final Identity identity) throws Exception {
Response res = response;
for (final Pass pass : this.passes) {
res = pass.exit(res, identity);
}
return res;
}
}