Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TkAppAuth |
|
| 1.5;1.5 | ||||
TkAppAuth$1 |
|
| 1.5;1.5 | ||||
TkAppAuth$FakePass |
|
| 1.5;1.5 |
1 | /** | |
2 | * Copyright (c) 2014-2015, Thindeck.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the thindeck.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.thindeck.cockpit; | |
31 | ||
32 | import com.jcabi.immutable.ArrayMap; | |
33 | import com.jcabi.manifests.Manifests; | |
34 | import java.io.IOException; | |
35 | import java.util.Iterator; | |
36 | import org.takes.Request; | |
37 | import org.takes.Response; | |
38 | import org.takes.Take; | |
39 | import org.takes.facets.auth.Identity; | |
40 | import org.takes.facets.auth.Pass; | |
41 | import org.takes.facets.auth.PsByFlag; | |
42 | import org.takes.facets.auth.PsChain; | |
43 | import org.takes.facets.auth.PsCookie; | |
44 | import org.takes.facets.auth.PsLogout; | |
45 | import org.takes.facets.auth.TkAuth; | |
46 | import org.takes.facets.auth.codecs.CcCompact; | |
47 | import org.takes.facets.auth.codecs.CcHex; | |
48 | import org.takes.facets.auth.codecs.CcSafe; | |
49 | import org.takes.facets.auth.codecs.CcSalted; | |
50 | import org.takes.facets.auth.codecs.CcXOR; | |
51 | import org.takes.facets.auth.social.PsGithub; | |
52 | import org.takes.misc.Opt; | |
53 | import org.takes.rq.RqHref; | |
54 | import org.takes.tk.TkWrap; | |
55 | ||
56 | /** | |
57 | * App auth. | |
58 | * | |
59 | * @author Yegor Bugayenko (yegor@teamed.io) | |
60 | * @version $Id$ | |
61 | * @since 0.5 | |
62 | * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) | |
63 | */ | |
64 | 1 | final class TkAppAuth extends TkWrap { |
65 | ||
66 | /** | |
67 | * Testing mode is ON? | |
68 | * @checkstyle ConstantUsageCheck (4 lines) | |
69 | */ | |
70 | 1 | private static final boolean TESTING = |
71 | Manifests.read("Thindeck-DynamoKey").startsWith("AAAA"); | |
72 | ||
73 | /** | |
74 | * Ctor. | |
75 | * @param take Take | |
76 | */ | |
77 | TkAppAuth(final Take take) { | |
78 | 1 | super(TkAppAuth.make(take)); |
79 | 1 | } |
80 | ||
81 | /** | |
82 | * Authenticated. | |
83 | * @param take Take | |
84 | * @return Authenticated take | |
85 | */ | |
86 | private static Take make(final Take take) { | |
87 | 1 | return new TkAuth( |
88 | take, | |
89 | new PsChain( | |
90 | new TkAppAuth.FakePass(), | |
91 | new PsByFlag( | |
92 | new PsByFlag.Pair( | |
93 | PsGithub.class.getSimpleName(), | |
94 | new PsGithub( | |
95 | Manifests.read("Thindeck-GithubId"), | |
96 | Manifests.read("Thindeck-GithubSecret") | |
97 | ) | |
98 | ), | |
99 | new PsByFlag.Pair( | |
100 | "fake-user", | |
101 | new TkAppAuth.FakePass() | |
102 | ), | |
103 | new PsByFlag.Pair( | |
104 | PsLogout.class.getSimpleName(), | |
105 | new PsLogout() | |
106 | ) | |
107 | ), | |
108 | new PsCookie( | |
109 | new CcSafe( | |
110 | new CcHex( | |
111 | new CcXOR( | |
112 | new CcSalted(new CcCompact()), | |
113 | Manifests.read("Thindeck-SecurityKey") | |
114 | ) | |
115 | ) | |
116 | ) | |
117 | ) | |
118 | ) | |
119 | ); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Fake pass. | |
124 | */ | |
125 | 4 | private static final class FakePass implements Pass { |
126 | @Override | |
127 | public Opt<Identity> enter(final Request req) throws IOException { | |
128 | final Opt<Identity> user; | |
129 | 1 | if (TkAppAuth.TESTING) { |
130 | 1 | final Iterator<String> login = |
131 | new RqHref.Base(req).href().param("user").iterator(); | |
132 | final String name; | |
133 | 1 | if (login.hasNext()) { |
134 | 0 | name = login.next(); |
135 | } else { | |
136 | 1 | name = "tester"; |
137 | } | |
138 | 1 | user = new Opt.Single<Identity>( |
139 | new Identity.Simple( | |
140 | String.format("urn:test:%s", name), | |
141 | new ArrayMap<String, String>().with("login", name) | |
142 | ) | |
143 | ); | |
144 | 1 | } else { |
145 | 0 | user = new Opt.Empty<>(); |
146 | } | |
147 | 1 | return user; |
148 | } | |
149 | @Override | |
150 | public Response exit(final Response response, final Identity identity) { | |
151 | 1 | return response; |
152 | } | |
153 | } | |
154 | } |