// Authenticate checks the access token is valid
func (s *Service) Authenticate(token string) (*AccessToken, error) {
	// Fetch the access token from the database
	accessToken := new(AccessToken)
	notFound := s.db.Where("token = ?", token).First(accessToken).RecordNotFound()

	// Not found
	if notFound {
		return nil, ErrAccessTokenNotFound
	}

	// Check the access token hasn't expired
	if time.Now().UTC().After(accessToken.ExpiresAt) {
		return nil, ErrAccessTokenExpired
	}

	// Extend refresh token expiration database
	query := s.db.Model(new(RefreshToken)).Where("client_id = ?", accessToken.ClientID.Int64)
	if accessToken.UserID.Valid {
		query = query.Where("user_id = ?", accessToken.UserID.Int64)
	} else {
		query = query.Where("user_id IS NULL")
	}
	increasedExpiresAt := gorm.NowFunc().Add(
		time.Duration(s.cnf.Oauth.RefreshTokenLifetime) * time.Second,
	)
	if err := query.UpdateColumn("expires_at", increasedExpiresAt).Error; err != nil {
		return nil, err
	}

	return accessToken, nil
}
func (s *Service) sendInvitationEmail(invitation *Invitation) error {
	invitationEmail, err := s.emailFactory.NewInvitationEmail(invitation)
	if err != nil {
		return fmt.Errorf("New invitation email error: %s", err)
	}

	// Try to send the invitation email
	if err := s.emailService.Send(invitationEmail); err != nil {
		return fmt.Errorf("Send email error: %s", err)
	}

	// If the email was sent successfully, update the email_sent flag
	now := gorm.NowFunc()
	if err := s.db.Model(invitation).UpdateColumns(Invitation{
		EmailTokenModel: EmailTokenModel{
			EmailSent:   true,
			EmailSentAt: &now,
			Model:       gorm.Model{UpdatedAt: time.Now().UTC()},
		},
	}).Error; err != nil {
		return err
	}

	s.Notify()

	return nil
}
func (s *Service) sendPasswordResetEmail(passwordReset *PasswordReset) error {
	passwordResetEmail, err := s.emailFactory.NewPasswordResetEmail(passwordReset)
	if err != nil {
		return fmt.Errorf("New password reset email error: %s", err)
	}

	// Try to send the password reset email
	if err := s.emailService.Send(passwordResetEmail); err != nil {
		return fmt.Errorf("Send email error: %s", err)
	}

	// If the email was sent successfully, update the email_sent flag
	now := gorm.NowFunc()
	if err := s.db.Model(passwordReset).UpdateColumns(PasswordReset{
		EmailTokenModel: EmailTokenModel{
			EmailSent:   true,
			EmailSentAt: &now,
			Model:       gorm.Model{UpdatedAt: now},
		},
	}).Error; err != nil {
		return nil
	}

	s.Notify()

	return nil
}
Exemple #4
0
func deleteScope(scope *gorm.Scope) {
	if !scope.HasError() {
		_, supportedModel := scope.InstanceGet("publish:supported_model")
		if supportedModel && IsDraftMode(scope.DB()) {
			scope.Raw(
				fmt.Sprintf("UPDATE %v SET deleted_at=%v, publish_status=%v %v",
					scope.QuotedTableName(),
					scope.AddToVars(gorm.NowFunc()),
					scope.AddToVars(DIRTY),
					scope.CombinedConditionSql(),
				))
			scope.Exec()
		} else {
			gorm.Delete(scope)
		}
	}
}
Exemple #5
0
// Print -
func (logger Logger) Print(values ...interface{}) {
	if len(values) > 1 {
		level := values[0]
		currentTime := "\n" + gorm.NowFunc().Format("2006-01-02 15:04:05")
		source := fmt.Sprintf("%v:", values[1])
		messages := []interface{}{source, currentTime}

		if level == "sql" {
			// duration
			messages = append(messages, fmt.Sprintf(" [%.2fms] ", float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0))
			// sql
			var formatedValues []interface{}
			for _, value := range values[4].([]interface{}) {
				indirectValue := reflect.Indirect(reflect.ValueOf(value))
				if indirectValue.IsValid() {
					value = indirectValue.Interface()
					if t, ok := value.(time.Time); ok {
						formatedValues = append(formatedValues, fmt.Sprintf("'%v'", t.Format(time.RFC3339)))
					} else if b, ok := value.([]byte); ok {
						formatedValues = append(formatedValues, fmt.Sprintf("'{...}%d'", len(b)))
						// formatedValues = append(formatedValues, fmt.Sprintf("'%v'", string(b)))
					} else if r, ok := value.(driver.Valuer); ok {
						if value, err := r.Value(); err == nil && value != nil {
							formatedValues = append(formatedValues, fmt.Sprintf("'...'"))
							// formatedValues = append(formatedValues, fmt.Sprintf("'%v'", value))
						} else {
							formatedValues = append(formatedValues, "NULL")
						}
					} else {
						formatedValues = append(formatedValues, fmt.Sprintf("'%v'", value))
					}
				} else {
					formatedValues = append(formatedValues, fmt.Sprintf("'%v'", value))
				}
			}
			messages = append(messages, fmt.Sprintf(sqlRegexp.ReplaceAllString(values[3].(string), "%v"), formatedValues...))
		} else {
			messages = append(messages, values[2:]...)
		}
		logger.Println(messages...)
	}
}