This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
gdk-core/src/main/java/com/github/libgraviton/gdk/auth/BasicAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.libgraviton.gdk.auth; | ||
|
||
import com.github.libgraviton.gdk.api.header.HeaderBag; | ||
import okio.ByteString; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
|
||
public class BasicAuth implements HeaderAuth { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(BasicAuth.class); | ||
|
||
private String username; | ||
private String password; | ||
|
||
public BasicAuth(String username, String password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
@Override | ||
public void addHeader(HeaderBag.Builder headerBuilder) { | ||
LOG.debug("Add basic auth header to request"); | ||
headerBuilder.set("Authorization", basic()); | ||
} | ||
|
||
public String basic() { | ||
return basic(Charset.forName("ISO-8859-1")); | ||
} | ||
|
||
public String basic(Charset charset) { | ||
String usernameAndPassword = username + ":" + password; | ||
byte[] bytes = usernameAndPassword.getBytes(charset); | ||
String encoded = ByteString.of(bytes).base64(); | ||
return "Basic " + encoded; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
gdk-core/src/main/java/com/github/libgraviton/gdk/auth/HeaderAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.github.libgraviton.gdk.auth; | ||
|
||
import com.github.libgraviton.gdk.api.header.HeaderBag; | ||
|
||
public interface HeaderAuth { | ||
|
||
void addHeader(HeaderBag.Builder headerBuilder); | ||
} |
15 changes: 15 additions & 0 deletions
15
gdk-core/src/main/java/com/github/libgraviton/gdk/auth/NoAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.libgraviton.gdk.auth; | ||
|
||
import com.github.libgraviton.gdk.api.header.HeaderBag; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class NoAuth implements HeaderAuth { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(NoAuth.class); | ||
|
||
@Override | ||
public void addHeader(HeaderBag.Builder headerBuilder) { | ||
LOG.debug("No auth selected."); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
gdk-core/src/test/java/com/github/libgraviton/gdk/auth/BasicAuthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.github.libgraviton.gdk.auth; | ||
|
||
import com.github.libgraviton.gdk.api.header.Header; | ||
import com.github.libgraviton.gdk.api.header.HeaderBag; | ||
import com.github.libgraviton.gdk.exception.CommunicationException; | ||
import org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class BasicAuthTest { | ||
|
||
@Test | ||
public void testEncodedCredentials() throws CommunicationException { | ||
String username = "asdf"; | ||
String password = "112233"; | ||
BasicAuth auth = new BasicAuth(username, password); | ||
|
||
String encodedCredentials = auth.basic(); | ||
assertEquals("Basic YXNkZjoxMTIyMzM=", encodedCredentials); | ||
} | ||
|
||
@Test | ||
public void testAddedHeader() throws CommunicationException { | ||
String username = "asdf"; | ||
String password = "112233"; | ||
BasicAuth auth = new BasicAuth(username, password); | ||
|
||
HeaderBag.Builder builder = new HeaderBag.Builder(); | ||
auth.addHeader(builder); | ||
|
||
Map<String, Header> headers = builder.build().all(); | ||
assertEquals(1, headers.size()); | ||
assertNotNull(headers.get("Authorization")); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
gdk-core/src/test/java/com/github/libgraviton/gdk/auth/NoAuthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.github.libgraviton.gdk.auth; | ||
|
||
import com.github.libgraviton.gdk.api.header.Header; | ||
import com.github.libgraviton.gdk.api.header.HeaderBag; | ||
import com.github.libgraviton.gdk.exception.CommunicationException; | ||
import org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class NoAuthTest { | ||
|
||
@Test | ||
public void testNoAddedHeader() throws CommunicationException { | ||
NoAuth auth = new NoAuth(); | ||
|
||
HeaderBag.Builder builder = new HeaderBag.Builder(); | ||
auth.addHeader(builder); | ||
|
||
Map<String, Header> headers = builder.build().all(); | ||
assertEquals(0, headers.size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters