...

Source file src/edge-infra.dev/pkg/edge/iam/storage/database/storage_profile.go

Documentation: edge-infra.dev/pkg/edge/iam/storage/database

     1  package database
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"edge-infra.dev/pkg/edge/iam/config"
    10  	"edge-infra.dev/pkg/edge/iam/profile"
    11  	"edge-infra.dev/pkg/edge/iam/util"
    12  
    13  	"github.com/pkg/errors"
    14  
    15  	"time"
    16  )
    17  
    18  func (s *Store) CreateAlias(ctx context.Context, alias, subject string) (err error) {
    19  	key := keyFrom(KeyPrefixAlias, alias)
    20  	temp := make(map[string]string)
    21  	temp["subject"] = strings.ToLower(subject)
    22  	payload, err := json.Marshal(temp)
    23  	if err != nil {
    24  		return errors.WithStack(err)
    25  	}
    26  	if err := s.createDoc(ctx, key, payload); err != nil {
    27  		return errors.Wrap(err, "failed to create alias")
    28  	}
    29  	return nil
    30  }
    31  func (s *Store) GetSubjectFromAlias(ctx context.Context, alias string) (subject string, err error) {
    32  	key := keyFrom(KeyPrefixAlias, alias)
    33  	var doc *Doc
    34  	if doc, err = s.getDoc(ctx, key); err != nil {
    35  		return "", errors.WithMessage(err, "failed to retrieve subject")
    36  	}
    37  	if doc == nil {
    38  		return "", errors.New("alias not found")
    39  	}
    40  
    41  	data := doc.Value
    42  	var value map[string]string
    43  	err = json.Unmarshal(data, &value)
    44  	if err != nil {
    45  		return "", errors.WithMessage(err, "schema not valid")
    46  	}
    47  	return strings.ToLower(value["subject"]), nil
    48  }
    49  func (s *Store) CreateIdentityProfile(ctx context.Context, p profile.Profile) error {
    50  	key := keyFrom(KeyPrefixProfile, p.Subject)
    51  	p.LastUpdated = time.Now().Unix()
    52  
    53  	payload, err := json.Marshal(p)
    54  	if err != nil {
    55  		return errors.WithStack(err)
    56  	}
    57  
    58  	if err := s.updateDoc(ctx, key, payload, WithExpiration(config.GetProfileTTL())); err != nil {
    59  		return errors.Wrap(err, "failed to save profile")
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func (s *Store) GetIdentityProfile(ctx context.Context, subject string) (profile *profile.Profile, err error) {
    66  	key := keyFrom(KeyPrefixProfile, subject)
    67  	var doc *Doc
    68  	if doc, err = s.getDoc(ctx, key); err != nil {
    69  		return nil, errors.WithMessage(err, "failed to retrieve profile")
    70  	}
    71  	if doc == nil {
    72  		return nil, nil
    73  	}
    74  
    75  	data := doc.Value
    76  	err = json.Unmarshal(data, &profile)
    77  	if err != nil {
    78  		return nil, errors.WithMessage(err, "profile schema not valid")
    79  	}
    80  
    81  	return profile, nil
    82  }
    83  
    84  func (s *Store) ExpireIdentityProfile(ctx context.Context, subject string) (err error) {
    85  	profile, err := s.GetIdentityProfile(ctx, subject)
    86  	if err != nil {
    87  		return errors.WithStack(err)
    88  	}
    89  	if profile == nil {
    90  		return fmt.Errorf("no such profile")
    91  	}
    92  
    93  	// set the profile last updated to a day ago...
    94  	lastUpdated := time.Unix(profile.LastUpdated, 0)
    95  	dayBefore := lastUpdated.AddDate(0, 0, -1)
    96  	profile.LastUpdated = dayBefore.Unix()
    97  
    98  	marshalledProfile, err := json.Marshal(profile)
    99  	if err != nil {
   100  		return errors.WithStack(err)
   101  	}
   102  
   103  	// save the updated profile
   104  	key := keyFrom(KeyPrefixProfile, subject)
   105  	if err := s.updateDoc(ctx, key, marshalledProfile); err != nil {
   106  		return errors.Wrap(err, "failed to save profile")
   107  	}
   108  
   109  	return nil
   110  }
   111  func (s *Store) AddAliasToProfile(ctx context.Context, userProfile *profile.Profile) error {
   112  	alias, _ := util.RandomStringGenerator(8)
   113  	err := s.CreateAlias(ctx, alias, userProfile.Subject)
   114  	if err != nil {
   115  		return errors.Wrap(err, "failed to create alias")
   116  	}
   117  	userProfile.Alias = alias
   118  	err = s.CreateIdentityProfile(ctx, *userProfile)
   119  	if err != nil {
   120  		return errors.Wrap(err, "failed to update profile with alias")
   121  	}
   122  	return nil
   123  }
   124  
   125  // type UserProfile struct {
   126  // 	Subject      string `json:"sub"`
   127  // 	Roles        string `json:"rls"`
   128  // 	Organization string `json:"org"`
   129  // 	LastUpdated  int64  `json:"last_updated"`
   130  // }
   131  
   132  // type Profile struct {
   133  // 	Subject      string `json:"sub"`
   134  // 	Organization string `json:"org"`
   135  // 	Roles        string `json:"rls"`
   136  // 	LastUpdated  int64  `json:"last_updated"`
   137  // }
   138  
   139  // func (s *Store) SaveProfile(ctx context.Context, p Profile) error {
   140  // 	key := keyFrom(KeyPrefixProfile, p.Subject)
   141  // 	p.LastUpdated = time.Now().Unix()
   142  
   143  // 	payload, err := json.Marshal(p)
   144  // 	if err != nil {
   145  // 		return errors.WithStack(err)
   146  // 	}
   147  
   148  // 	if err := s.DB.Set(key, payload, 0).Err(); err != nil {
   149  // 		return errors.Wrap(err, "failed to save profile")
   150  // 	}
   151  
   152  // 	return nil
   153  // }
   154  
   155  // func (s *Store) IsProfileExists(sub string) (bool, error) {
   156  // 	key := keyFrom(KeyPrefixProfile, sub)
   157  
   158  // 	_, err := s.DB.Get(key).Result()
   159  // 	if err == redis.Nil {
   160  // 		return false, fmt.Errorf("profile not exist. %v", err)
   161  // 	} else if err != nil {
   162  // 		return false, errors.WithStack(err)
   163  // 	}
   164  
   165  // 	return true, nil
   166  // }
   167  
   168  // func (s *Store) GetProfile(sub string) (*UserProfile, error) {
   169  // 	key := keyFrom(KeyPrefixProfile, sub)
   170  
   171  // 	res, err := s.DB.Get(key).Result()
   172  // 	if err != nil {
   173  // 		return nil, fmt.Errorf("failed to get profile. %v", err)
   174  // 	}
   175  
   176  // 	var schema UserProfile
   177  // 	if err := json.Unmarshal([]byte(res), &schema); err != nil {
   178  // 		return nil, err
   179  // 	}
   180  
   181  // 	return &schema, nil
   182  // }
   183  
   184  // func (s *Store) MarkProfileAsExpired(sub string) error {
   185  // 	key := keyFrom(KeyPrefixProfile, sub)
   186  
   187  // 	res, err := s.DB.Get(key).Result()
   188  // 	if err != nil {
   189  // 		return fmt.Errorf("failed to get profile. %v", err)
   190  // 	}
   191  
   192  // 	var profile UserProfile
   193  // 	if err := json.Unmarshal([]byte(res), &profile); err != nil {
   194  // 		return err
   195  // 	}
   196  
   197  // 	t := time.Unix(profile.LastUpdated, 0)
   198  
   199  // 	dayBack := t.AddDate(0, 0, -1)
   200  // 	profile.LastUpdated = dayBack.Unix()
   201  
   202  // 	marshalledProfile, err := json.Marshal(profile)
   203  // 	if err != nil {
   204  // 		return errors.WithStack(err)
   205  // 	}
   206  
   207  // 	if err := s.DB.Set(key, marshalledProfile, 0).Err(); err != nil {
   208  // 		return errors.Wrap(err, "failed to save profile")
   209  // 	}
   210  
   211  // 	return nil
   212  // }
   213  

View as plain text