auth

package
v6.2.5 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

auth

An auth plugin for verifying peer at the first time.

Test
package auth_test

import (
	"testing"
	"time"

	tp "github.com/henrylee2cn/teleport/v6"
	"github.com/henrylee2cn/teleport/v6/plugin/auth"
)

func Test(t *testing.T) {
	// Server
	srv := tp.NewPeer(
		tp.PeerConfig{ListenPort: 9090},
		authChecker,
	)
	srv.RouteCall(new(Home))
	go srv.ListenAndServe()
	time.Sleep(1e9)

	// Client
	cli := tp.NewPeer(
		tp.PeerConfig{},
		authBearer,
	)
	sess, stat := cli.Dial(":9090")
	if !stat.OK() {
		t.Fatal(stat)
	}
	var result interface{}
	stat = sess.Call("/home/test",
		map[string]string{
			"author": "henrylee2cn",
		},
		&result,
		tp.WithAddMeta("peer_id", "110"),
	).Status()
	if !stat.OK() {
		t.Error(stat)
	}
	t.Logf("result:%v", result)
	time.Sleep(3e9)
}

const clientAuthInfo = "client-auth-info-12345"

var authBearer = auth.NewBearerPlugin(
	func(sess auth.Session, fn auth.SendOnce) (stat *tp.Status) {
		var ret string
		stat = fn(clientAuthInfo, &ret)
		if !stat.OK() {
			return
		}
		tp.Infof("auth info: %s, result: %s", clientAuthInfo, ret)
		return
	},
	tp.WithBodyCodec('s'),
)

var authChecker = auth.NewCheckerPlugin(
	func(sess auth.Session, fn auth.RecvOnce) (ret interface{}, stat *tp.Status) {
		var authInfo string
		stat = fn(&authInfo)
		if !stat.OK() {
			return
		}
		tp.Infof("auth info: %v", authInfo)
		if clientAuthInfo != authInfo {
			return nil, tp.NewStatus(403, "auth fail", "auth fail detail")
		}
		return "pass", nil
	},
	tp.WithBodyCodec('s'),
)

type Home struct {
	tp.CallCtx
}

func (h *Home) Test(arg *map[string]string) (map[string]interface{}, *tp.Status) {
	return map[string]interface{}{
		"arg": *arg,
	}, nil
}

test command:

go test -v -run=Test

Documentation

Overview

Package auth is a plugin for verifying peer at the first time.

Copyright 2017 HenryLee. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var MultiRecvErr = tp.NewStatus(
	tp.CodeInternalServerError,
	"auth-checker plugin usage is incorrect",
	"multiple call RecvOnce function",
)

MultiRecvErr the error of multiple call RecvOnce function

View Source
var MultiSendErr = tp.NewStatus(
	tp.CodeWriteFailed,
	"auth-bearer plugin usage is incorrect",
	"multiple call SendOnce function",
)

MultiSendErr the error of multiple call SendOnce function

Functions

func NewBearerPlugin

func NewBearerPlugin(fn Bearer, infoSetting ...tp.MessageSetting) tp.Plugin

NewBearerPlugin creates a auth bearer plugin for client.

func NewCheckerPlugin

func NewCheckerPlugin(fn Checker, retSetting ...tp.MessageSetting) tp.Plugin

NewCheckerPlugin creates a auth checker plugin for server.

Types

type Bearer

type Bearer func(sess Session, fn SendOnce) *tp.Status

Bearer initiates an authorization request and handles the response.

type Checker

type Checker func(sess Session, fn RecvOnce) (ret interface{}, stat *tp.Status)

Checker checks the authorization request.

type RecvOnce

type RecvOnce func(infoRecv interface{}) *tp.Status

RecvOnce receives authorization request once.

type SendOnce

type SendOnce func(info, retRecv interface{}) *tp.Status

SendOnce sends authorization request once.

type Session

type Session interface {
	// Peer returns the peer.
	Peer() tp.Peer
	// SetID sets the session id.
	SetID(newID string)
	// LocalAddr returns the local network address.
	LocalAddr() net.Addr
	// RemoteAddr returns the remote network address.
	RemoteAddr() net.Addr
	// Swap returns custom data swap of the session(socket).
	Swap() goutil.Map
}

Session auth session provides SetID, RemoteAddr and Swap methods in base session

Jump to

Keyboard shortcuts

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