github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/authorizations/service.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package authorizations
    19  
    20  import (
    21  	"github.com/aacfactory/errors"
    22  	"github.com/aacfactory/fns/services"
    23  	"time"
    24  )
    25  
    26  var (
    27  	endpointName = []byte("authorizations")
    28  )
    29  
    30  type Options struct {
    31  	encoder TokenEncoder
    32  	store   TokenStore
    33  }
    34  
    35  type Option func(options *Options)
    36  
    37  func WithTokenEncoder(encoder TokenEncoder) Option {
    38  	return func(options *Options) {
    39  		options.encoder = encoder
    40  	}
    41  }
    42  
    43  func WithTokenStore(store TokenStore) Option {
    44  	return func(options *Options) {
    45  		options.store = store
    46  	}
    47  }
    48  
    49  func New(options ...Option) services.Service {
    50  	opt := Options{
    51  		encoder: &defaultTokenEncoder{},
    52  		store:   &defaultTokenStore{},
    53  	}
    54  	for _, option := range options {
    55  		option(&opt)
    56  	}
    57  	return &service{
    58  		Abstract: services.NewAbstract(string(endpointName), true, opt.encoder, opt.store),
    59  		encoder:  opt.encoder,
    60  		store:    opt.store,
    61  	}
    62  }
    63  
    64  // service
    65  // use @authorization
    66  type service struct {
    67  	services.Abstract
    68  	encoder TokenEncoder
    69  	store   TokenStore
    70  }
    71  
    72  func (svc *service) Construct(options services.Options) (err error) {
    73  	config := Config{}
    74  	configErr := options.Config.As(&config)
    75  	if configErr != nil {
    76  		err = errors.Warning("fns: authorizations construct failed").WithCause(configErr)
    77  		return
    78  	}
    79  	if config.ExpireTTL < 1 {
    80  		config.ExpireTTL = 3 * 24 * time.Hour
    81  	}
    82  	if config.AutoRefresh {
    83  		if config.AutoRefreshWindow < 1 || config.AutoRefreshWindow >= config.ExpireTTL {
    84  			config.AutoRefreshWindow = config.ExpireTTL / 10
    85  		}
    86  	}
    87  	err = svc.Abstract.Construct(options)
    88  	if err != nil {
    89  		return
    90  	}
    91  	svc.AddFunction(&validateFn{
    92  		encoder:       svc.encoder,
    93  		store:         svc.store,
    94  		autoRefresh:   config.AutoRefresh,
    95  		refreshWindow: config.AutoRefreshWindow,
    96  		expireTTL:     config.ExpireTTL,
    97  	})
    98  	svc.AddFunction(&createFn{
    99  		encoder:   svc.encoder,
   100  		store:     svc.store,
   101  		expireTTL: config.ExpireTTL,
   102  	})
   103  	svc.AddFunction(&listFn{
   104  		store: svc.store,
   105  	})
   106  	svc.AddFunction(&removeFn{
   107  		store: svc.store,
   108  	})
   109  	return
   110  }