This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
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
165 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
@file:Suppress("EXPERIMENTAL_API_USAGE") | ||
|
||
package dev.koding.copilot.auth | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import dev.koding.copilot.config.settings | ||
import dev.koding.copilot.httpClient | ||
import dev.koding.copilot.util.Notifications | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import java.awt.Desktop | ||
import java.net.URI | ||
import javax.swing.JOptionPane | ||
|
||
const val authenticateUrl = "https://vscode-auth.github.com/authorize/" + | ||
"?callbackUri=vscode://vscode.github-authentication/did-authenticate" + | ||
"&scope=read:user" + | ||
"&responseType=code" + | ||
"&authServer=https://github.com" | ||
|
||
data class GitHubTokenResponse( | ||
@SerializedName("access_token") | ||
val accessToken: String?, | ||
@SerializedName("token_type") | ||
val tokenType: String?, | ||
val scope: String? | ||
) | ||
|
||
data class CopilotTokenResponse( | ||
val token: String, | ||
@SerializedName("expires_at") | ||
val expiry: Int | ||
) | ||
|
||
private suspend fun getAuthToken(url: String): CopilotTokenResponse { | ||
val code = Url(url).parameters["code"] ?: error("Code not present") | ||
|
||
val tokenResponse = httpClient.post<GitHubTokenResponse>("https://vscode-auth.github.com/token") { | ||
parameter("code", code) | ||
accept(ContentType.Application.Json) | ||
} | ||
|
||
return httpClient.get("https://api.github.com/copilot_internal/token") { | ||
header("Authorization", "token ${tokenResponse.accessToken ?: error("Invalid GitHub token")}") | ||
} | ||
} | ||
|
||
fun handleLogin(handler: (String) -> Unit = { Notifications.send("Login successful") }) { | ||
Desktop.getDesktop().browse(URI.create(authenticateUrl)) | ||
val url = JOptionPane.showInputDialog("Enter the callback URL") | ||
|
||
// TODO: Change from global scope | ||
GlobalScope.launch { | ||
val token = getAuthToken(url).token | ||
settings.token = token | ||
handler(token) | ||
} | ||
} |
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,36 @@ | ||
package dev.koding.copilot.util | ||
|
||
import com.intellij.notification.NotificationGroupManager | ||
import com.intellij.notification.NotificationType | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
|
||
object Notifications { | ||
|
||
private val group = NotificationGroupManager.getInstance().getNotificationGroup("GitHub Copilot") | ||
private var shown = mutableSetOf<String>() | ||
|
||
@Suppress("DialogTitleCapitalization") | ||
fun send( | ||
message: String, | ||
type: NotificationType = NotificationType.INFORMATION, | ||
once: Boolean = false, | ||
vararg actions: NotificationAction | ||
) { | ||
if (once && message in shown) return | ||
group.createNotification(message, type) | ||
.apply { | ||
actions.forEach { | ||
addAction(object : AnAction(it.name) { | ||
override fun actionPerformed(e: AnActionEvent) = it.action() | ||
}) | ||
} | ||
shown += message | ||
}.notify(null) | ||
} | ||
|
||
data class NotificationAction( | ||
val name: String, | ||
val action: () -> Unit | ||
) | ||
} |
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
Oops, something went wrong.