goline

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2021 License: MIT Imports: 8 Imported by: 2

README

Go Report Card Go Reference

Goline - Simple LINE Login for Go

Support API

Install

go get "github.com/jlandowner/goline"
Example

call verify-id-token API

package main

import (
	"context"
	"flag"
	"log"
	"net/http"

	"github.com/jlandowner/goline"
)

func main() {
	var clientid, idtoken string
	flag.StringVar(&clientid, "clientid", "", "LINE Channel ID https://developers.line.biz/ja/reference/line-login/#verify-id-token")
	flag.StringVar(&idtoken, "idtoken", "", "ID Token https://developers.line.biz/ja/reference/line-login/#verify-id-token")
	flag.Parse()

	ctx := context.TODO()

	line := goline.Client{Client: http.DefaultClient}

	p, err := line.VerifyIDToken(ctx, clientid, idtoken, "")
	if err != nil {
		log.Fatalln(err)
	}

	log.Println("LINE User Name", p.Name)
}
Use http Middleware

This package prepares http Middleware easy to integrate LINE Login in your http server.

Here is a example server

package main

import (
	"errors"
	"flag"
	"log"
	"net/http"

	"github.com/go-logr/zapr"
	"github.com/gorilla/mux"
	"github.com/jlandowner/goline"
	"go.uber.org/zap"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
	// Get User name
	name := r.Header.Get(goline.HeaderKeyLINEDisplayName)

	log.Println("hello,", name)
	w.Write([]byte("hello," + name))
}

func main() {
	clientid := flag.String("clientid", "", "LINE Channel ID https://developers.line.biz/ja/reference/line-login/#verify-id-token")
	flag.Parse()

	router := mux.NewRouter()
	router.HandleFunc("/hello", helloHandler)

	// Setup logr
	zapLog, err := zap.NewDevelopment()
	if err != nil {
		panic(err)
	}
	log := zapr.NewLogger(zapLog)

	// Setup Client
	lineClient := &goline.Client{Client: http.DefaultClient}

	// Setup Authorizer
	lineAuth := goline.NewAuthorizer(*clientid, lineClient, zapr.NewLogger(zapLog))

	// Use VerifyIDTokenMiddleware
	router.Use(lineAuth.VerifyIDTokenMiddleware)

	// Or Use VerifyAccessTokenMiddleware
	// router.Use(lauth.VerifyAccessTokenMiddleware)

	err = http.ListenAndServe(":3000", router)
	if !errors.Is(err, http.ErrServerClosed) {
		log.Error(err, "unexpected err")
	}
}
# Run server
$ go run main.go --clientid="LINE Channel ID" &

# Request without token
$ curl -i http://localhost:3000/hello
400 Bad Request

# Request with valid token
# This assume you have already got ID Token in your client apps (Webclient(JavaScript), Android, iOS or others)
$ idtoken="ID Token"
$ curl -i http://localhost:3000/hello -H "Authorization: Bearer $idtoken"
200 OK

hello, XXX

Documentation

Index

Constants

View Source
const (
	HeaderKeyLINEUserID        = "LINEUserID"
	HeaderKeyLINEDisplayName   = "LINEDisplayName"
	HeaderKeyLINEPictureURL    = "LINEPictureURL"
	HeaderKeyLINEEmail         = "LINEEmail"
	HeaderKeyLINEStatusMessage = "LINEStatusMessage"
)

Variables

View Source
var (
	// ErrBadRequest 400 Bad Request リクエストに問題があります。リクエストパラメータとJSONの形式を確認してください。
	ErrBadRequest = errors.New("400 Bad Request")
	// ErrUnauthorized 401 Unauthorized Authorizationヘッダーを正しく送信していることを確認してください。
	ErrUnauthorized = errors.New("401 Unauthorized")
	// ErrForbidden 403 Forbidden APIを使用する権限がありません。ご契約中のプランやアカウントに付与されている権限を確認してください。
	ErrForbidden = errors.New("403 Forbidden")
	// ErrTooManyRequests 429 Too Many Requests リクエスト頻度をレート制限内に抑えてください。
	ErrTooManyRequests = errors.New("429 Too Many Requests")
	// ErrInternalServerError 500 Internal Server Error APIサーバーの一時的なエラーです。
	ErrInternalServerError = errors.New("500 Internal Server Error")
)

Functions

This section is empty.

Types

type Authorizer

type Authorizer struct {
	// contains filtered or unexported fields
}

Authorizer is a clientset of LINE Auth API

func NewAuthorizer

func NewAuthorizer(lineClient *Client, log logr.Logger) *Authorizer

NewAuthorizer return new Authorizer

func (*Authorizer) VerifyAccessTokenMiddleware

func (a *Authorizer) VerifyAccessTokenMiddleware(next http.Handler) http.Handler

VerifyAccessTokenMiddleware is a middleware of http handler Obtain access token from authorization header and verify it upstream The authorized LINE user info is set in request headers "LINEUserID", "LINEDisplayName", "LINEPictureURL", "LINEStatusMessage"

func (*Authorizer) VerifyIDTokenMiddleware

func (a *Authorizer) VerifyIDTokenMiddleware(next http.Handler) http.Handler

VerifyIDTokenMiddleware is a middleware of http handler Obtain id token from authorization header and verify it upstream The authorized LINE user info is set in request headers "LINEUserID", "LINEDisplayName", "LINEPictureURL", "LINEEmail"

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an http client access to LINE Login API

func NewClient added in v0.0.2

func NewClient(clientid string, client *http.Client) *Client

NewClient returns LINE loging API Client. "id" is LINE Client ID a.k.a LINE Channel ID.

func (*Client) GetProfile

func (c *Client) GetProfile(ctx context.Context, accessToken string) (*LINEProfile, error)

GetProfile is a function to call get-user-profile API https://developers.line.biz/ja/reference/line-login-v2/#get-profile-response

func (*Client) VerifyAccessToken

func (c *Client) VerifyAccessToken(ctx context.Context, accessToken string) (*VerifyAccessTokenResponse, error)

VerifyAccessToken is a function to call verify-access-token API https://developers.line.biz/ja/reference/line-login/#verify-access-token

func (*Client) VerifyIDToken

func (c *Client) VerifyIDToken(ctx context.Context, idToken, userid, nonce string) (*IDTokenData, error)

VerifyIDToken is a function to call verify-id-token. UserID and Nonce can be empty when not use. https://developers.line.biz/ja/reference/line-login/#verify-id-token

type IDTokenData

type IDTokenData struct {
	Iss     string   `json:"iss"`
	Sub     string   `json:"sub"`
	Aud     string   `json:"aud"`
	Exp     string   `json:"exp"`
	Nonce   string   `json:"nonce,omitempty"`
	Amr     []string `json:"amr,omitempty"`
	Name    string   `json:"name,omitempty"`
	Picutre string   `json:"picture,omitempty"`
	Email   string   `json:"email,omitempty"`
}

IDTokenData is the response json struct of verify-id-token API. https://developers.line.biz/ja/reference/line-login/#verify-id-token

type LINEProfile

type LINEProfile struct {
	UserID        string `json:"userId"`
	DisplayName   string `json:"displayName"`
	PictureURL    string `json:"pictureUrl"`
	StatusMessage string `json:"statusMessage"`
}

LINEProfile is the response json struct of get-user-profile API https://developers.line.biz/ja/reference/line-login-v2/#get-profile-response

type VerifyAccessTokenResponse

type VerifyAccessTokenResponse struct {
	Scope     string `json:"scope"`
	ClientID  string `json:"client_id"`
	ExpiresIn int    `json:"expires_in"`
}

VerifyAccessTokenResponse is the response json struct of verify-access-token API. https://developers.line.biz/ja/reference/line-login/#verify-access-token

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL