Skip to content

Instantly share code, notes, and snippets.

@ilsubyeega
Last active February 19, 2022 13:47
Show Gist options
  • Select an option

  • Save ilsubyeega/98ac030b4d473dab7dceb976f171e2cb to your computer and use it in GitHub Desktop.

Select an option

Save ilsubyeega/98ac030b4d473dab7dceb976f171e2cb to your computer and use it in GitHub Desktop.
meiling-prototype
import ng.meili.client.*;
val client = MeilingClient("https://internal.meiling.roto.win/")
val pkceChallenge = MeilingPKCEChallenge(Random().nextLong().toString())
val codecredbuilder = MeilingCodeCredentialBuilder("ab5e527f-2d11-4b93-b9ba-33081bc8eec5", listOf("openid", "email"), pkceChallenge)
val url = client.codeCredentialsUrl(codecredbuilder, "http://localhost:8080/callback");
val response_code = "xxxxxxxxxxx"
val clientCred = MeilingClientCredentials("ab5e527f-2d11-4b93-b9ba-33081bc8eec5", "secret")
val codecred = client.exchangeCodeCredentials(clientCred, response_code, pkceChallenge) // MeilingCredentials
val refresh = codecred.refresh()
val accessToken = codecred.access
val refreshToken = codecred.refresh
// 이게 끝~
// async 형태 만들어야됨

MeiliNG Wrapper code style feedback

코드 자체는 이해하기 더 쉽게 TypeScript로 작성되었으나, 주 목표는 Kotlin에서 작성하고 이후에 호환 예정.

Import

import { MeilingClient } from 'meili-ng'

const client = new MeilingClient() // 기본값은 https://meiling.stella-api.dev
const client = new MeilingClient("https://meiling.debugman.win")

OAuth2 Login/Logout

With Authorization Code

// 여기서 클래스 이름 고민중
import { MeilingCredentials, MeilingAuthorizationMethod } from 'meili-ng'
const credential_one = new MeilingCredentials({
  authMethod: MeilingAuthorizationMethod.CODE,
  clientId: "ab5e527f-2d11-4b93-b9ba-33081bc8eec5",
  scopes: ["openid", "email"]
})

return credential_one.generateUrl()
// https://accounts.stella-it.com/auth?client_id=ab5e527f-2d11-4b93-b9ba-33081bc8eec5&response_type=code&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=openid%20email
// 혹은 
return credential_one.generateUrl("other_redirect_url")

// code를 획득하였다면 다음과 같이 작성
const credential_two = new MeilingCredentials({
  authMethod: MeilingAuthorizationMethod.AUTHORIZATION_CODE,
  clientId: "ab5e527f-2d11-4b93-b9ba-33081bc8eec5",
  clientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxx"
})
await credential_two.authorize("insert_code_here")
// = { access_token = "xxxxxx", refresh_token = "xxxxxx", expires_in = 36000 }

With Authorization Code (With Security)

// 여기서 클래스 이름 고민중
import { MeilingCredentials, MeilingAuthorizationMethod } from 'meili-ng'
const challenge = "challenge-text"
const credential_one = new MeilingCredentials({
  authMethod: MeilingAuthorizationMethod.CODE,
  clientId: "ab5e527f-2d11-4b93-b9ba-33081bc8eec5",
  scopes: ["openid", "email"],
  challenge: challenge // sdk will automaticlly hash the value and pass into the query string
})

return credential_one.generateUrl()
// https://accounts.stella-it.com/auth?client_id=ab5e527f-2d11-4b93-b9ba-33081bc8eec5&response_type=code&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=openid%20email
// 혹은 
return credential_one.generateUrl("other_redirect_url")

// code를 획득하였다면 다음과 같이 작성
const credential_two = new MeilingCredentials({
  authMethod: MeilingAuthorizationMethod.AUTHORIZATION_CODE,
  clientId: "ab5e527f-2d11-4b93-b9ba-33081bc8eec5",
  clientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxx"
})
await credential_two.authorize("insert_code_here", challenge)
// = { access_token = "xxxxxx", refresh_token = "xxxxxx", expires_in = 36000 }

With Refresh Token

// 토큰 작성 생략
const oauthData = new MeilingOAuthData(await credential.authorize(....))
oauthData.refresh()

Logout

await credential.revoke()
// This will destroy every property in credential instance.

Usage

client.setOAuthData(oauthData)
await client.get("https://connect.roto.win/callback") // = HttpResponse
await client.get("https://connect.roto.win/callback", null, {
  header: [
    'accept-encoding': 'utf8'
  ]
}) // = HttpResponse
await client.post("https://connect.roto.win/callback", { 
  random: objected
}) // = HttpResponse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment