tastypie

package module
v0.0.0-...-b1ab2c1 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2018 License: MIT Imports: 8 Imported by: 0

README

go-pg-tastypie

Go Report Card GoDoc

Documentation

Overview

Package tastypie contains a Django Tastypie (https://django-tastypie.readthedocs.io/) models and functions, that helps to integrate Go project with existing Django Tastypie project.

Index

Examples

Constants

View Source
const APIKeyAuthorizationHeader = "Authorization"

APIKeyAuthorizationHeader is a key in HTTP header where is stored authorization key

View Source
const (
	TypeAPIKey = 1 << iota // APIKey Authentication
)

Authentication types

Variables

View Source
var (
	ErrorInvalidAuthenticationType  = errors.New("Invalid authentication type")
	ErrorInvalidAuthorizationHeader = errors.New("Invalid Authorization header")
	ErrorInvalidCredentials         = errors.New("Invalid credentials")
	ErrorUserIsInactive             = errors.New("User is inactive")
)

Predefined errors

Functions

This section is empty.

Types

type APIAccess

type APIAccess struct {
	TableName string `sql:"tastypie_apiaccess"`

	ID            uint16
	Identifier    string `sql:"type:varchar(255),notnull"`
	URL           string `sql:"type:varchar(255),notnull,default:''"`
	RequestMethod string `sql:"type:varchar(10),notnull,default:''"`
	Accessed      int64  `sql:",notnull"`
}

APIAccess represents tastypie_apiaccess table

func (*APIAccess) BeforeInsert

func (a *APIAccess) BeforeInsert(db orm.DB) error

BeforeInsert hook update "Accessed" field

func (*APIAccess) BeforeUpdate

func (a *APIAccess) BeforeUpdate(db orm.DB) error

BeforeUpdate hook update "Accessed" field

func (APIAccess) String

func (a APIAccess) String() string
Example
row := APIAccess{
	ID:            1,
	Identifier:    "test",
	URL:           "https://www.example.com/",
	RequestMethod: "GET",
	Accessed:      123456,
}

fmt.Println(row)
Output:

test @ 123456

type APIKey

type APIKey struct {
	TableName string `sql:"tastypie_apikey"`

	ID      uint16
	UserID  uint16 `sql:",notnull" pg:",fk:auth.User"`
	User    *auth.User
	Key     string    `sql:"type:varchar(128),notnull,default:''"`
	Created time.Time `sql:",notnull"`
}

APIKey represents tastypie_apikey table

func (APIKey) String

func (a APIKey) String() string
Example
user := auth.User{
	Username: "admin",
}
row := APIKey{
	ID:      1,
	User:    &user,
	Key:     "qaz123",
	Created: time.Now(),
}

fmt.Println(row)
Output:

qaz123 for admin

type APIKeyAuthentication

type APIKeyAuthentication struct {
	// Handler do database connection
	DB *pg.DB
}

APIKeyAuthentication represents authentication based on API Key

func (APIKeyAuthentication) ExtractCredentials

func (a APIKeyAuthentication) ExtractCredentials(r *http.Request) (string, string, error)

ExtractCredentials returns username and tastypie apikey extracted from request

Example
r, _ := http.NewRequest(http.MethodGet, "http://www.example.com/", nil)
r.Header.Set("Authorization", "ApiKey admin:qaz123")
authentication := APIKeyAuthentication{}

username, key, _ := authentication.ExtractCredentials(r)
fmt.Println(username, key)
Output:

admin qaz123

func (APIKeyAuthentication) IsAuthenticated

func (a APIKeyAuthentication) IsAuthenticated(r *http.Request) (*auth.User, error)

IsAuthenticated checks if user is authenticated and return it

type Authentication

type Authentication interface {
	// Extract credentials from request
	ExtractCredentials(r *http.Request) (string, string, error)

	// Checks if user is authenticated and return it
	IsAuthenticated(r *http.Request) (*auth.User, error)
}

Authentication is a interface to various authentication backends

func GetAuthentication

func GetAuthentication(authenticationType int, db *pg.DB) (Authentication, error)

GetAuthentication gets Authentication object based on type and initiates it

Jump to

Keyboard shortcuts

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