Skip to content

Commit

Permalink
#320 RqWithAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed May 29, 2015
1 parent 3b09cd1 commit 43b3a19
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 7 deletions.
118 changes: 118 additions & 0 deletions src/main/java/org/takes/facets/auth/RqWithAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.facets.auth;

import java.io.IOException;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.facets.auth.codecs.CcPlain;
import org.takes.rq.RqFake;
import org.takes.rq.RqWithHeader;
import org.takes.rq.RqWrap;

/**
* Request with already authenticated identity.
*
* <p>This class is very useful for unit testing, when you need to
* test a "take" that requires a request to contain an already
* authenticated user.
*
* <p>The class is immutable and thread-safe.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.18
*/
@EqualsAndHashCode(callSuper = true)
public final class RqWithAuth extends RqWrap {

/**
* Ctor.
* @param urn URN of the tester
* @throws IOException If fails
*/
public RqWithAuth(final String urn) throws IOException {
this(new Identity.Simple(urn));
}

/**
* Ctor.
* @param identity Identity
* @throws IOException If fails
*/
public RqWithAuth(final Identity identity) throws IOException {
this(identity, new RqFake());
}

/**
* Ctor.
* @param urn URN of the tester
* @param req Request
* @throws IOException If fails
*/
public RqWithAuth(final String urn, final Request req) throws IOException {
this(new Identity.Simple(urn), req);
}

/**
* Ctor.
* @param identity Identity
* @param req Request
* @throws IOException If fails
*/
public RqWithAuth(final Identity identity, final Request req)
throws IOException {
this(identity, TkAuth.class.getSimpleName(), req);
}

/**
* Ctor.
* @param identity Identity
* @param header Header name
* @param req Request
* @throws IOException If fails
*/
public RqWithAuth(final Identity identity, final String header,
final Request req) throws IOException {
super(RqWithAuth.make(identity, header, req));
}

/**
* Ctor.
* @param identity Identity
* @param header Header name
* @param req Request
* @return Request
* @throws IOException If fails
*/
private static Request make(final Identity identity, final String header,
final Request req) throws IOException {
return new RqWithHeader(
req,
header,
new String(new CcPlain().encode(identity))
);
}

}
8 changes: 1 addition & 7 deletions src/main/java/org/takes/facets/auth/TkAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.facets.auth.codecs.CcPlain;
import org.takes.misc.Opt;
import org.takes.rq.RqWithHeader;
import org.takes.rq.RqWithoutHeader;

/**
Expand Down Expand Up @@ -104,11 +102,7 @@ private Response act(final Request req, final Identity identity)
throws IOException {
Request wrap = new RqWithoutHeader(req, this.header);
if (!identity.equals(Identity.ANONYMOUS)) {
wrap = new RqWithHeader(
wrap,
this.header,
new String(new CcPlain().encode(identity))
);
wrap = new RqWithAuth(identity, this.header, wrap);
}
return this.pass.exit(this.origin.act(wrap), identity);
}
Expand Down

0 comments on commit 43b3a19

Please sign in to comment.