Skip to content

Instantly share code, notes, and snippets.

@SealOfTime
Created May 1, 2021 19:32
Show Gist options
  • Save SealOfTime/059b6311f57af48cf8277c0f648f15bc to your computer and use it in GitHub Desktop.
Save SealOfTime/059b6311f57af48cf8277c0f648f15bc to your computer and use it in GitHub Desktop.
package com.example.auth
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.http.*
import io.ktor.locations.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
val loginProviders = listOf(
OAuthServerSettings.OAuth2ServerSettings(
name = "vk",
authorizeUrl = "https://oauth.vk.com/authorize",
accessTokenUrl = "https://oauth.vk.com/access_token",
clientId = "айди",
clientSecret = "секрет",
defaultScopes = listOf("email"),
authorizeUrlInterceptor = { print(this) }
)
).associateBy {it.name}
@Location("/login/{type?}") class login(val type: String = "", val redirectUri: String = "/")
fun Application.configureSecurity(){
install(Authentication){
oauth("vk") {
client = HttpClient(Apache)
providerLookup = { loginProviders[application.locations.resolve<login>(login::class, this).type]}
urlProvider = { url(login(it.name, this.request.uri))}
}
}
routing {
authenticate("vk") {
location<login>(){
param("error") {
handle {
call.response.status(HttpStatusCode.ExpectationFailed)
call.respond("fail")
}
}
handle {
val principal = call.authentication.principal<OAuthAccessTokenResponse.OAuth2>()
if (principal != null) {
call.response.status(HttpStatusCode.OK)
call.respond(principal.extraParameters.entries())
} else {
call.respondRedirect(application.locations.href(login("vk")))
}
}
}
get("/locked"){
val principal = call.authentication.principal<OAuthAccessTokenResponse.OAuth2>()//ожидаю, что здесь не должно быть нулла
call.respondText {
"Oh hi mark!"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment