package main

import (
	"context"
	"fmt"
	"github.com/tidbcloud/tiinf/sdk/auth"
	"log"
)

func Example_GetIdentityInfo() {
	// you need init account client first, the account client is singleton, you can call it in init() method
	auth.InitAccountClientByEndpoint("account-provider-grpc:10000", 0)

	// identity type
	tye := auth.AuthnIdentityTypeUserToken
	// identity value
	val := "12345"
	param := auth.GetIdentityParam{
		IdentityType:  tye,
		IdentityValue: val,
	}
	info, err := auth.GetIdentityInfo(context.Background(), &param)
	log.Println(fmt.Sprintf("GetIdentityInfo ret, info: %s, err: %s", info, err))
}

func Example_CheckIdentityPermission() {
	// you need init account client first, the account client is singleton, you can call it in init() method
	auth.InitAccountClientByEndpoint("account-provider-grpc:10000", 0)

	param := auth.CheckPermissionParam{
		IdentityTye: auth.AccessIdentityTypeUserID,
		IdentityVal: "12345",
		ResourceUri: "/orgs/{org_id}/projects/{pid}/list",
		ResourceParam: map[string]string{
			"org_id": "11",
			"pid":    "22",
		},
	}
	info, err := auth.CheckIdentityPermission(context.Background(), &param)
	log.Println(fmt.Sprintf("CheckIdentityPermission ret, info: %v, err: %s", info, err))
}

func Example_CheckRegionApiKeyPermission() {
	// identity
	identity := "access_key1"
	// request relate resource_uri
	resource := "/dataapi/dataapps/{app_id}/action"
	// params in resource_uri, for example: org_id、project_id，if resource has no variable, the resourceParam can be empty
	resourceParam := map[string]string{
		// key must be variable in resource_uri,
		"app_id": "123",
	}
	auth.InitAccountClientByEndpoint("account-provider-grpc:10000", 0)

	param := auth.CheckPermissionParam{
		IdentityTye:      auth.AccessIdentityTypeApiKeyAK,
		IdentityVal:      identity,
		ResourceUri:      resource,
		ResourceParam:    resourceParam,
		GoDDBPermitCheck: true,
	}
	info, err := auth.CheckIdentityPermission(context.Background(), &param)
	log.Println(fmt.Sprintf("CheckIdentityPermission ret, info: %v, err: %s", info, err))
}
