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
7 changed files
with
109 additions
and
13 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
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
48 changes: 48 additions & 0 deletions
48
bearerauth-core/src/main/java/io/github/vpavic/bearerauth/WwwAuthenticateBuilder.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,48 @@ | ||
package io.github.vpavic.bearerauth; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public final class WwwAuthenticateBuilder { | ||
|
||
private final BearerTokenException bearerTokenException; | ||
|
||
private String realm; | ||
|
||
private WwwAuthenticateBuilder(BearerTokenException bearerTokenException) { | ||
Objects.requireNonNull(bearerTokenException, "bearerTokenException must not be null"); | ||
this.bearerTokenException = bearerTokenException; | ||
} | ||
|
||
public static WwwAuthenticateBuilder from(BearerTokenException bearerTokenException) { | ||
return new WwwAuthenticateBuilder(bearerTokenException); | ||
} | ||
|
||
public WwwAuthenticateBuilder withRealm(String realm) { | ||
Objects.requireNonNull(realm, "realm must not be null"); | ||
this.realm = realm; | ||
return this; | ||
} | ||
|
||
public String build() { | ||
String wwwAuthenticate = "Bearer"; | ||
List<String> attributes = new ArrayList<>(); | ||
if (this.realm != null) { | ||
attributes.add(buildAttribute("realm", this.realm)); | ||
} | ||
BearerTokenError error = this.bearerTokenException.getError(); | ||
if (error != null) { | ||
attributes.add(buildAttribute("error", error.getCode())); | ||
} | ||
if (!attributes.isEmpty()) { | ||
wwwAuthenticate += " " + String.join(", ", attributes); | ||
} | ||
return wwwAuthenticate; | ||
} | ||
|
||
private static String buildAttribute(String name, String value) { | ||
return name + "=\"" + value + "\""; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
bearerauth-core/src/test/java/io/github/vpavic/bearerauth/WwwAuthenticateBuilderTests.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,5 @@ | ||
package io.github.vpavic.bearerauth; | ||
|
||
class WwwAuthenticateBuilderTests { | ||
|
||
} |
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