azure ad cli

https://teamsmiley.gitbook.io/devops/azure/active-directory

์—ฌ๊ธฐ์— ์ด์–ด์„œ ๋งŒ๋“ ๋‹ค

๊ธฐ์กด cli๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. https://teamsmiley.gitbook.io/devops/go-lang/create-cli

ํ† ํฐ ๋ฐ›์•„์˜ค๊ธฐ

package httpApi

import (
  "context"
  "log"

  msal "github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
  "github.com/spf13/viper"
)

const scope = "api://xxxxx/.default"
const clientID = "xxxxx"

func getSecret() string {
  sec := viper.GetString("AZURE_SECRET")
  if sec == "" {
    log.Fatalf("AZURE_SECRET is missing, please configure it myctl configure --secret <secret>")
  }
  return sec
}

type tokenProvider struct{}

func (t *tokenProvider) OAuthToken() string {

  clientCredential, err := msal.NewCredFromSecret(getSecret())

  if err != nil {
    log.Fatalf("Couldn't create client app (%s)", err)
  }
  app, err := msal.New(clientID, clientCredential, func(o *msal.Options) {
    o.Authority = "https://login.microsoftonline.com/xxxxxxx"
  })
  if err != nil {
    log.Fatalf("Couldn't get application (%s)", err)
  }

  ctx := context.Background()
  token, err := app.AcquireTokenByCredential(ctx, []string{
    scope,
  })
  if err != nil {
    log.Fatalf("Error getting token (%s)", err)
  }
  return token.AccessToken
}

xxxx๋œ ๋ถ€๋ถ„์„ ์ „๋ถ€ ์ˆ˜์ •ํ•ด์ค€๋‹ค.

์ด์ œ http ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ๋ณด๋‚ด๊ธฐ ์ „์— ํ˜ธ์ถœํ•ด์„œ ํ† ํฐ์„ ๋งŒ๋“ ํ›„ ํ† ํฐ์„ http ๋ฆฌํ€˜์ŠคํŠธ์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

์ด์ œ myctl์„ ์‚ฌ์šฉํ•˜๋ฉด ์–ธ์ œ๋“  ํ† ํฐ์„ ๋ฐ›์•„์™€์„œ ๊ทธ ํ† ํฐ์„ ์ด์šฉํ•˜์—ฌ api์— ์š”์ฒญํ• ์ˆ˜ ์žˆ๋‹ค.

์ด๊ฑธ ์‚ฌ์šฉํ•˜๋ฉด ์ด์ œ api์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„์ˆ˜๋„ ์“ธ์ˆ˜๋„ ์žˆ๊ฒŒ ๋œ๋‹ค.

Last updated

Was this helpful?