github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/auth.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received c copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package controllers
    20  
    21  import (
    22  	"github.com/e154/smart-home/api/stub"
    23  	"github.com/labstack/echo/v4"
    24  
    25  	"github.com/e154/smart-home/common"
    26  	"github.com/e154/smart-home/common/apperr"
    27  	m "github.com/e154/smart-home/models"
    28  )
    29  
    30  // ControllerAuth ...
    31  type ControllerAuth struct {
    32  	*ControllerCommon
    33  }
    34  
    35  // NewControllerAuth ...
    36  func NewControllerAuth(common *ControllerCommon) *ControllerAuth {
    37  	return &ControllerAuth{
    38  		ControllerCommon: common,
    39  	}
    40  }
    41  
    42  // Signin ...
    43  func (c ControllerAuth) AuthServiceSignin(ctx echo.Context) error {
    44  
    45  	username, pass, _ := c.parseBasicAuth(ctx.Request().Header.Get("authorization"))
    46  
    47  	var user *m.User
    48  	var accessToken string
    49  
    50  	var ip string
    51  	if _ip := ctx.Request().Header.Get("ip"); _ip != "" {
    52  		if ok, _ := c.validation.ValidVar(_ip, "ip", "required,ipv4"); ok {
    53  			ip = _ip
    54  		}
    55  	}
    56  
    57  	var err error
    58  	if user, accessToken, err = c.endpoint.Auth.SignIn(ctx.Request().Context(), username, pass, ip); err != nil {
    59  		return c.ERROR(ctx, apperr.ErrUnauthorized)
    60  	}
    61  
    62  	currentUser := &stub.ApiCurrentUser{}
    63  	_ = common.Copy(&currentUser, &user, common.JsonEngine)
    64  
    65  	resp := &stub.ApiSigninResponse{
    66  		CurrentUser: currentUser,
    67  		AccessToken: accessToken,
    68  	}
    69  
    70  	return c.HTTP200(ctx, ResponseWithObj(ctx, resp))
    71  }
    72  
    73  // Signout ...
    74  func (c ControllerAuth) AuthServiceSignout(ctx echo.Context) error {
    75  
    76  	currentUser, err := c.currentUser(ctx)
    77  	if err != nil {
    78  		return c.ERROR(ctx, apperr.ErrUnauthorized)
    79  	}
    80  
    81  	if err = c.endpoint.Auth.SignOut(ctx.Request().Context(), currentUser); err != nil {
    82  		return c.ERROR(ctx, apperr.ErrUnauthorized)
    83  	}
    84  
    85  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
    86  }
    87  
    88  // AccessList ...
    89  func (c ControllerAuth) AuthServiceAccessList(ctx echo.Context) error {
    90  
    91  	currentUser, err := c.currentUser(ctx)
    92  	if err != nil {
    93  		return c.ERROR(ctx, apperr.ErrUnauthorized)
    94  	}
    95  
    96  	accessList, err := c.endpoint.Auth.AccessList(ctx.Request().Context(), currentUser, c.accessList)
    97  	if err != nil {
    98  		return c.ERROR(ctx, apperr.ErrUnauthorized)
    99  	}
   100  
   101  	resp := &stub.ApiAccessListResponse{
   102  		AccessList: c.dto.Role.ToAccessListResult(accessList),
   103  	}
   104  
   105  	return c.HTTP200(ctx, ResponseWithObj(ctx, resp))
   106  }
   107  
   108  // PasswordReset ...
   109  func (c ControllerAuth) AuthServicePasswordReset(ctx echo.Context, _ stub.AuthServicePasswordResetParams) error {
   110  
   111  	obj := &stub.ApiPasswordResetRequest{}
   112  	if err := c.Body(ctx, obj); err != nil {
   113  		return c.ERROR(ctx, err)
   114  	}
   115  
   116  	if err := c.endpoint.Auth.PasswordReset(ctx.Request().Context(), obj.Email, obj.Token, obj.NewPassword); err != nil {
   117  		return c.ERROR(ctx, apperr.ErrUserNotFound)
   118  	}
   119  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
   120  }