-
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
1 parent
75edc46
commit a0c1fb4
Showing
10 changed files
with
113 additions
and
18 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package http | ||
|
||
object CustomHeader { | ||
const val SESSION_ID = "session-id" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package routes | ||
|
||
import auth.Session | ||
import http.CustomHeader | ||
import http.INVALID_SESSION | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import java.util.* | ||
import util.ServerGlobals | ||
|
||
suspend fun requiresSession(call: ApplicationCall, fn: suspend (session: Session) -> Unit) { | ||
val sessionIdStr = | ||
call.request.header(CustomHeader.SESSION_ID) | ||
?: throw ClientException( | ||
HttpStatusCode.Unauthorized, | ||
INVALID_SESSION, | ||
"Missing session id", | ||
) | ||
|
||
val sessionId = | ||
try { | ||
UUID.fromString(sessionIdStr) | ||
} catch (e: IllegalArgumentException) { | ||
throw ClientException( | ||
HttpStatusCode.BadRequest, | ||
INVALID_SESSION, | ||
"Session ID was not a valid UUID", | ||
e, | ||
) | ||
} | ||
|
||
val session = | ||
ServerGlobals.sessionStore.find(sessionId) | ||
?: throw ClientException( | ||
HttpStatusCode.Unauthorized, | ||
INVALID_SESSION, | ||
"No session found for ID $sessionId", | ||
) | ||
|
||
fn(session) | ||
} |
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