github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/api/auth.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   */
     7  
     8  package api
     9  
    10  import (
    11  	"fmt"
    12  
    13  	"github.com/dghubble/sling"
    14  	"github.com/sirupsen/logrus"
    15  	"github.com/vchain-us/vcn/internal/errors"
    16  	"github.com/vchain-us/vcn/pkg/meta"
    17  )
    18  
    19  type authRequest struct {
    20  	Email    string `json:"email"`
    21  	Password string `json:"password"`
    22  	Otp      string `json:"otp"`
    23  }
    24  
    25  type tokenResponse struct {
    26  	Token string `token:"token"`
    27  }
    28  
    29  type publisherExistsResponse struct {
    30  	Exists bool `json:"exists"`
    31  }
    32  
    33  type publisherExistsParams struct {
    34  	Email string `url:"email"`
    35  }
    36  
    37  func publisherEndpoint() string {
    38  	return meta.APIEndpoint("publisher")
    39  }
    40  
    41  func checkUserExists(email string) (success bool, err error) {
    42  	response := new(publisherExistsResponse)
    43  	restError := new(Error)
    44  	r, err := sling.New().
    45  		Get(publisherEndpoint()+"/exists").
    46  		QueryStruct(&publisherExistsParams{Email: email}).
    47  		Receive(&response, restError)
    48  	logger().WithFields(logrus.Fields{
    49  		"response":  response,
    50  		"err":       err,
    51  		"restError": restError,
    52  	}).Trace("checkUserExists")
    53  	if err != nil {
    54  		return false, err
    55  	}
    56  	if r.StatusCode == 200 {
    57  		return response.Exists, nil
    58  	}
    59  	return false, fmt.Errorf("check publisher failed: %+v", restError)
    60  }
    61  
    62  func checkToken(token string) (success bool, err error) {
    63  	restError := new(Error)
    64  	response, err := newSling(token).
    65  		Get(publisherEndpoint()+"/auth/check").
    66  		Receive(nil, restError)
    67  	logger().WithFields(logrus.Fields{
    68  		"response":  response,
    69  		"err":       err,
    70  		"restError": restError,
    71  	}).Trace("checkToken")
    72  	if response != nil {
    73  		switch response.StatusCode {
    74  		case 200:
    75  			return true, nil
    76  		case 401:
    77  			fallthrough
    78  		case 403:
    79  			fallthrough
    80  		case 419:
    81  			return false, nil
    82  		}
    83  	}
    84  	if restError.Error != "" {
    85  		err = fmt.Errorf("%+v", restError)
    86  	}
    87  	return false, fmt.Errorf("check token failed: %s", err)
    88  }
    89  
    90  func authenticateUser(email string, password string, otp string) (token string, err error) {
    91  	response := new(tokenResponse)
    92  	restError := new(Error)
    93  	r, err := sling.New().
    94  		Post(publisherEndpoint()+"/auth").
    95  		BodyJSON(authRequest{Email: email, Password: password, Otp: otp}).
    96  		Receive(response, restError)
    97  	logger().WithFields(logrus.Fields{
    98  		"email":     email,
    99  		"response":  response,
   100  		"err":       err,
   101  		"restError": restError,
   102  	}).Trace("authenticateUser")
   103  	if err != nil {
   104  		return "", err
   105  	}
   106  	switch r.StatusCode {
   107  	case 200:
   108  		return response.Token, nil
   109  	case 400:
   110  		return "", fmt.Errorf(errors.UnconfirmedEmail, email, meta.DashboardURL())
   111  	case 401:
   112  		return "", fmt.Errorf("invalid password")
   113  	}
   114  	if restError.Error != "" {
   115  		err = fmt.Errorf("%+v", restError)
   116  	}
   117  	return "", fmt.Errorf("authentication failed: %s", err)
   118  }