-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from blogify-dev/dev
Merge dev into master for PRX3
- Loading branch information
Showing
109 changed files
with
3,004 additions
and
1,075 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{12} - %msg%n</pattern> | ||
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{12} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
<root level="trace"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
<logger name="org.eclipse.jetty" level="INFO"/> | ||
<logger name="io.netty" level="INFO"/> | ||
<logger name="com.zaxxer.hikari" level="INFO"/> | ||
</configuration> |
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,75 @@ | ||
package blogify.backend.auth.jwt | ||
|
||
import blogify.backend.resources.User | ||
import blogify.backend.services.UserService | ||
import blogify.backend.util.short | ||
import blogify.backend.util.toUUID | ||
|
||
import com.github.kittinunf.result.coroutines.SuspendableResult | ||
|
||
import com.andreapivetta.kolor.green | ||
import com.andreapivetta.kolor.red | ||
|
||
import io.jsonwebtoken.Claims | ||
import io.jsonwebtoken.Jws | ||
import io.jsonwebtoken.JwtException | ||
import io.jsonwebtoken.Jwts | ||
import io.jsonwebtoken.SignatureAlgorithm | ||
import io.jsonwebtoken.security.Keys | ||
import io.ktor.application.ApplicationCall | ||
|
||
import org.slf4j.LoggerFactory | ||
|
||
import java.util.Calendar | ||
import java.util.Date | ||
|
||
private val keyPair = Keys.keyPairFor(SignatureAlgorithm.ES512) | ||
|
||
private val logger = LoggerFactory.getLogger("blogify-auth-token") | ||
|
||
/** | ||
* Creates a [Jws] for the specific [user]. | ||
*/ | ||
fun generateJWT(user: User) = Jwts | ||
.builder() | ||
.setSubject(user.uuid.toString()) | ||
.setIssuer("blogify") | ||
.apply { | ||
val cal = Calendar.getInstance() | ||
|
||
cal.time = Date() | ||
cal.add(Calendar.DAY_OF_MONTH, +7) | ||
|
||
setExpiration(cal.time) | ||
} | ||
.signWith(keyPair.private).compact().also { | ||
logger.debug("${"created token for user with id".green()} {${user.uuid.toString().take(8)}...}") | ||
} | ||
|
||
/** | ||
* Validates a JWT, returning a [SuspendableResult] if that token authenticates a user, or an exception if the token is invalid | ||
*/ | ||
suspend fun validateJwt(callContext: ApplicationCall, token: String): SuspendableResult<User, Exception> { | ||
var jwsClaims: Jws<Claims>? = null | ||
|
||
try { | ||
jwsClaims = Jwts | ||
.parser() | ||
.setSigningKey(keyPair.public) | ||
.requireIssuer("blogify") | ||
.setAllowedClockSkewSeconds(1) | ||
.parseClaimsJws(token) | ||
} catch(e: JwtException) { | ||
logger.debug("${"invalid token attempted".red()} - ${e.javaClass.simpleName.takeLastWhile { it != '.' }}") | ||
e.printStackTrace() | ||
return SuspendableResult.error(e) | ||
} catch (e: Exception) { | ||
logger.debug("${"unknown exception during token validation -".red()} - ${e.javaClass.simpleName.takeLastWhile { it != '.' }}") | ||
e.printStackTrace() | ||
} | ||
|
||
val user = UserService.get(callContext, jwsClaims?.body?.subject?.toUUID() ?: error("malformed uuid in jwt")) | ||
logger.debug("got valid JWT for user {${user.get().uuid.short()}...}".green()) | ||
|
||
return SuspendableResult.of { user.get() } | ||
} |
Oops, something went wrong.