Skip to content

Commit 09ac0bb

Browse files
authored
Merge pull request #71 from myndocs/release/0.6.0
Release/0.6.0
2 parents 3f39a51 + ec6f848 commit 09ac0bb

File tree

48 files changed

+916
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+916
-200
lines changed

Jenkinsfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ pipeline {
1818
}
1919
}
2020
}
21+
post {
22+
always {
23+
cleanWs()
24+
}
25+
}
2126
}

README.md

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,37 @@ It encourages to adapt to existing implementations instead the other way around.
88
# Frameworks
99
## Setup
1010
### Maven
11-
First define the version to be used and set it as a property
11+
1212
```xml
1313
<properties>
14-
<myndocs.oauth.version>0.5.0</myndocs.oauth.version>
14+
<myndocs.oauth.version>0.6.0</myndocs.oauth.version>
1515
</properties>
16-
```
1716

18-
Include the following repository in order to download the artifacts
19-
```xml
17+
<dependencies>
18+
<dependency>
19+
<groupId>nl.myndocs</groupId>
20+
<artifactId>oauth2-server-core</artifactId>
21+
<version>${myndocs.oauth.version}</version>
22+
</dependency>
23+
24+
<!-- In memory dependencies -->
25+
<dependency>
26+
<groupId>nl.myndocs</groupId>
27+
<artifactId>oauth2-server-client-inmemory</artifactId>
28+
<version>${myndocs.oauth.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>nl.myndocs</groupId>
32+
<artifactId>oauth2-server-identity-inmemory</artifactId>
33+
<version>${myndocs.oauth.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>nl.myndocs</groupId>
37+
<artifactId>oauth2-server-token-store-inmemory</artifactId>
38+
<version>${myndocs.oauth.version}</version>
39+
</dependency>
40+
</dependencies>
41+
2042
<repositories>
2143
<repository>
2244
<id>myndocs-oauth2</id>
@@ -25,32 +47,22 @@ Include the following repository in order to download the artifacts
2547
</repositories>
2648
```
2749

28-
For the frameworks examples we need at least the following dependencies:
29-
```xml
30-
<dependency>
31-
<groupId>nl.myndocs</groupId>
32-
<artifactId>oauth2-server-core</artifactId>
33-
<version>${myndocs.oauth.version}</version>
34-
</dependency>
35-
36-
<!-- In memory dependencies -->
37-
<dependency>
38-
<groupId>nl.myndocs</groupId>
39-
<artifactId>oauth2-server-client-inmemory</artifactId>
40-
<version>${myndocs.oauth.version}</version>
41-
</dependency>
42-
<dependency>
43-
<groupId>nl.myndocs</groupId>
44-
<artifactId>oauth2-server-identity-inmemory</artifactId>
45-
<version>${myndocs.oauth.version}</version>
46-
</dependency>
47-
<dependency>
48-
<groupId>nl.myndocs</groupId>
49-
<artifactId>oauth2-server-token-store-inmemory</artifactId>
50-
<version>${myndocs.oauth.version}</version>
51-
</dependency>
50+
### Gradle
51+
```groovy
52+
repositories {
53+
maven { url 'https://dl.bintray.com/adhesivee/oauth2-server' }
54+
}
55+
56+
dependencies {
57+
compile "nl.myndocs:oauth2-server-core:$myndocs_oauth_version"
58+
// In memory dependencies
59+
compile "nl.myndocs:oauth2-server-client-inmemory:$myndocs_oauth_version"
60+
compile "nl.myndocs:oauth2-server-identity-inmemory:$myndocs_oauth_version"
61+
compile "nl.myndocs:oauth2-server-token-store-inmemory:$myndocs_oauth_version"
62+
}
5263
```
5364

65+
5466
### Framework implementation
5567
The following frameworks are supported:
5668
- [Ktor](docs/ktor.md)

docs/http4k.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# http4k
22

33
## Dependencies
4+
5+
### Maven
46
```xml
57
<dependency>
68
<groupId>nl.myndocs</groupId>
@@ -9,11 +11,17 @@
911
</dependency>
1012
```
1113

14+
### Gradle
15+
```groovy
16+
compile "nl.myndocs:oauth2-server-http4k:$myndocs_oauth_version"
17+
```
18+
19+
1220
## Implementation
1321
```kotlin
1422
val app: HttpHandler = routes(
1523
"/ping" bind GET to { _: Request -> Response(Status.OK).body("pong!") }
16-
) `enable oauth2` {
24+
).enableOauth2 {
1725
identityService = InMemoryIdentity()
1826
.identity {
1927
username = "foo"

docs/javalin.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Javalin
22

33
## Dependencies
4+
5+
### Maven
46
```xml
57
<dependency>
68
<groupId>nl.myndocs</groupId>
@@ -9,6 +11,11 @@
911
</dependency>
1012
```
1113

14+
### Gradle
15+
```groovy
16+
compile "nl.myndocs:oauth2-server-javalin:$myndocs_oauth_version"
17+
```
18+
1219
## Implementation
1320
```kotlin
1421
Javalin.create().apply {

docs/ktor.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Dependencies
44

5+
### Maven
56
```xml
67
<dependency>
78
<groupId>nl.myndocs</groupId>
@@ -10,6 +11,11 @@
1011
</dependency>
1112
```
1213

14+
### Gradle
15+
```groovy
16+
compile "nl.myndocs:oauth2-server-ktor:$myndocs_oauth_version"
17+
```
18+
1319
## Implementation
1420
```kotlin
1521
embeddedServer(Netty, 8080) {

docs/sparkjava.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Spark java
22

33
## Dependencies
4+
5+
### Maven
46
```xml
57
<dependency>
68
<groupId>nl.myndocs</groupId>
@@ -9,6 +11,11 @@
911
</dependency>
1012
```
1113

14+
### Gradle
15+
```groovy
16+
compile "nl.myndocs:oauth2-server-sparkjava:$myndocs_oauth_version"
17+
```
18+
1219
## Implementation
1320
```kotlin
1421
Oauth2Server.configureOauth2Server {

oauth2-server-client-inmemory/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>kotlin-oauth2-server</artifactId>
77
<groupId>nl.myndocs</groupId>
8-
<version>0.5.0</version>
8+
<version>0.6.0</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

oauth2-server-core/pom.xml

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,9 @@
55
<parent>
66
<artifactId>kotlin-oauth2-server</artifactId>
77
<groupId>nl.myndocs</groupId>
8-
<version>0.5.0</version>
8+
<version>0.6.0</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>oauth2-server-core</artifactId>
13-
14-
<dependencies>
15-
<dependency>
16-
<groupId>org.junit.jupiter</groupId>
17-
<artifactId>junit-jupiter-engine</artifactId>
18-
<version>5.2.0</version>
19-
<scope>test</scope>
20-
</dependency>
21-
<dependency>
22-
<groupId>io.mockk</groupId>
23-
<artifactId>mockk</artifactId>
24-
<version>1.8.12.kotlin13</version>
25-
<scope>test</scope>
26-
</dependency>
27-
<dependency>
28-
<groupId>org.hamcrest</groupId>
29-
<artifactId>hamcrest-library</artifactId>
30-
<version>1.3</version>
31-
<scope>test</scope>
32-
</dependency>
33-
</dependencies>
3413
</project>

oauth2-server-core/src/main/java/nl/myndocs/oauth2/CallRouter.kt

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package nl.myndocs.oauth2
22

3-
import nl.myndocs.oauth2.authenticator.Authorizer
3+
import nl.myndocs.oauth2.authenticator.Credentials
44
import nl.myndocs.oauth2.exception.*
55
import nl.myndocs.oauth2.grant.Granter
66
import nl.myndocs.oauth2.grant.GrantingCall
@@ -11,6 +11,8 @@ import nl.myndocs.oauth2.request.CallContext
1111
import nl.myndocs.oauth2.request.RedirectAuthorizationCodeRequest
1212
import nl.myndocs.oauth2.request.RedirectTokenRequest
1313
import nl.myndocs.oauth2.request.headerCaseInsensitive
14+
import nl.myndocs.oauth2.router.RedirectRouter
15+
import nl.myndocs.oauth2.router.RedirectRouterResponse
1416

1517
class CallRouter(
1618
val tokenEndpoint: String,
@@ -19,7 +21,7 @@ class CallRouter(
1921
private val tokenInfoCallback: (TokenInfo) -> Map<String, Any?>,
2022
private val granters: List<GrantingCall.() -> Granter>,
2123
private val grantingCallFactory: (CallContext) -> GrantingCall
22-
) {
24+
) : RedirectRouter {
2325
companion object {
2426
const val METHOD_POST = "post"
2527
const val METHOD_GET = "get"
@@ -29,16 +31,21 @@ class CallRouter(
2931

3032
}
3133

32-
fun route(
33-
callContext: CallContext,
34-
authorizer: Authorizer) {
34+
fun route(callContext: CallContext) {
3535
when (callContext.path) {
3636
tokenEndpoint -> routeTokenEndpoint(callContext)
37-
authorizeEndpoint -> routeAuthorizeEndpoint(callContext, authorizer)
3837
tokenInfoEndpoint -> routeTokenInfoEndpoint(callContext)
3938
}
4039
}
4140

41+
override fun route(callContext: CallContext, credentials: Credentials?): RedirectRouterResponse {
42+
return when (callContext.path) {
43+
authorizeEndpoint -> routeAuthorizeEndpoint(callContext, credentials)
44+
else -> throw NoRoutesFoundException("Route '${callContext.path}' not found")
45+
}
46+
}
47+
48+
4249
private fun routeTokenEndpoint(callContext: CallContext) {
4350
if (callContext.method.toLowerCase() != METHOD_POST) {
4451
return
@@ -72,21 +79,18 @@ class CallRouter(
7279

7380
fun routeAuthorizationCodeRedirect(
7481
callContext: CallContext,
75-
authorizer: Authorizer
76-
) {
82+
credentials: Credentials?
83+
): RedirectRouterResponse {
7784
val queryParameters = callContext.queryParameters
78-
val credentials = authorizer.extractCredentials()
7985
try {
8086
val redirect = grantingCallFactory(callContext).redirect(
8187
RedirectAuthorizationCodeRequest(
8288
queryParameters["client_id"],
8389
queryParameters["redirect_uri"],
84-
credentials?.username ?: "",
85-
credentials?.password ?: "",
90+
credentials?.username,
91+
credentials?.password,
8692
queryParameters["scope"]
87-
),
88-
authorizer.authenticator(),
89-
authorizer.scopesVerifier()
93+
)
9094
)
9195

9296
var stateQueryParameter = ""
@@ -96,31 +100,31 @@ class CallRouter(
96100
}
97101

98102
callContext.redirect(queryParameters["redirect_uri"] + "?code=${redirect.codeToken}$stateQueryParameter")
103+
104+
return RedirectRouterResponse(true)
99105
} catch (unverifiedIdentityException: InvalidIdentityException) {
100106
callContext.respondStatus(STATUS_UNAUTHORIZED)
101-
authorizer.failedAuthentication()
107+
108+
return RedirectRouterResponse(false)
102109
}
103110
}
104111

105112

106113
fun routeAccessTokenRedirect(
107114
callContext: CallContext,
108-
authorizer: Authorizer
109-
) {
115+
credentials: Credentials?
116+
): RedirectRouterResponse {
110117
val queryParameters = callContext.queryParameters
111-
val credentials = authorizer.extractCredentials()
112118

113119
try {
114120
val redirect = grantingCallFactory(callContext).redirect(
115121
RedirectTokenRequest(
116122
queryParameters["client_id"],
117123
queryParameters["redirect_uri"],
118-
credentials?.username ?: "",
119-
credentials?.password ?: "",
124+
credentials?.username,
125+
credentials?.password,
120126
queryParameters["scope"]
121-
),
122-
authorizer.authenticator(),
123-
authorizer.scopesVerifier()
127+
)
124128
)
125129

126130
var stateQueryParameter = ""
@@ -134,33 +138,33 @@ class CallRouter(
134138
"&token_type=bearer&expires_in=${redirect.expiresIn()}$stateQueryParameter"
135139
)
136140

141+
return RedirectRouterResponse(true)
137142
} catch (unverifiedIdentityException: InvalidIdentityException) {
138-
authorizer.failedAuthentication()
139143
callContext.respondStatus(STATUS_UNAUTHORIZED)
144+
145+
return RedirectRouterResponse(false)
140146
}
141147
}
142148

143-
private fun routeAuthorizeEndpoint(callContext: CallContext, authorizer: Authorizer) {
149+
private fun routeAuthorizeEndpoint(callContext: CallContext, credentials: Credentials?): RedirectRouterResponse {
144150
try {
145-
if (callContext.method.toLowerCase() != METHOD_GET) {
146-
return
151+
if (!arrayOf(METHOD_GET, METHOD_POST).contains(callContext.method.toLowerCase())) {
152+
return RedirectRouterResponse(false)
147153
}
148154

149-
val allowedResponseTypes = setOf("code", "token")
150155
val responseType = callContext.queryParameters["response_type"]
151156
?: throw InvalidRequestException("'response_type' not given")
152157

153-
if (!allowedResponseTypes.contains(responseType)) {
154-
throw InvalidGrantException("'grant_type' with value '$responseType' not allowed")
155-
}
156-
157-
when (responseType) {
158-
"code" -> routeAuthorizationCodeRedirect(callContext, authorizer)
159-
"token" -> routeAccessTokenRedirect(callContext, authorizer)
158+
return when (responseType) {
159+
"code" -> routeAuthorizationCodeRedirect(callContext, credentials)
160+
"token" -> routeAccessTokenRedirect(callContext, credentials)
161+
else -> throw InvalidGrantException("'grant_type' with value '$responseType' not allowed")
160162
}
161163
} catch (oauthException: OauthException) {
162164
callContext.respondStatus(STATUS_BAD_REQUEST)
163165
callContext.respondJson(oauthException.toMap())
166+
167+
return RedirectRouterResponse(false)
164168
}
165169
}
166170

0 commit comments

Comments
 (0)