github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/auth/token/basic/basic.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/util/token/basic/basic.go
    16  
    17  package basic
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/google/uuid"
    25  	"github.com/tickoalcantara12/micro/v3/service/auth"
    26  	"github.com/tickoalcantara12/micro/v3/service/store"
    27  	"github.com/tickoalcantara12/micro/v3/util/auth/token"
    28  )
    29  
    30  // Basic implementation of token provider, backed by the store
    31  type Basic struct {
    32  	store store.Store
    33  }
    34  
    35  var (
    36  	// StorePrefix to isolate tokens
    37  	StorePrefix = "tokens/"
    38  )
    39  
    40  // NewTokenProvider returns an initialized basic provider
    41  func NewTokenProvider(opts ...token.Option) token.Provider {
    42  	options := token.NewOptions(opts...)
    43  
    44  	if options.Store == nil {
    45  		options.Store = store.DefaultStore
    46  	}
    47  
    48  	return &Basic{
    49  		store: options.Store,
    50  	}
    51  }
    52  
    53  // Generate a token for an account
    54  func (b *Basic) Generate(acc *auth.Account, opts ...token.GenerateOption) (*token.Token, error) {
    55  	options := token.NewGenerateOptions(opts...)
    56  
    57  	// marshal the account to bytes
    58  	bytes, err := json.Marshal(acc)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	// write to the store
    64  	key := uuid.New().String()
    65  	err = b.store.Write(&store.Record{
    66  		Key:    fmt.Sprintf("%v%v", StorePrefix, key),
    67  		Value:  bytes,
    68  		Expiry: options.Expiry,
    69  	})
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	// return the token
    75  	return &token.Token{
    76  		Token:   key,
    77  		Created: time.Now(),
    78  		Expiry:  time.Now().Add(options.Expiry),
    79  	}, nil
    80  }
    81  
    82  // Inspect a token
    83  func (b *Basic) Inspect(t string) (*auth.Account, error) {
    84  	// lookup the token in the store
    85  	recs, err := b.store.Read(StorePrefix + t)
    86  	if err == store.ErrNotFound {
    87  		return nil, token.ErrInvalidToken
    88  	} else if err != nil {
    89  		return nil, err
    90  	}
    91  	bytes := recs[0].Value
    92  
    93  	// unmarshal the bytes
    94  	var acc *auth.Account
    95  	if err := json.Unmarshal(bytes, &acc); err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	return acc, nil
   100  }
   101  
   102  // String returns basic
   103  func (b *Basic) String() string {
   104  	return "basic"
   105  }