| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | package oauth | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-25 15:15:36 +02:00
										 |  |  | 	"code.superseriousbusiness.org/oauth2/v4" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/oauth2/v4/models" | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	"codeberg.org/gruf/go-mutexes" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtserror" | 
					
						
							| 
									
										
										
										
											2021-09-01 11:45:01 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/id" | 
					
						
							| 
									
										
										
										
											2022-07-19 09:47:55 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/state" | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-22 22:26:54 +01:00
										 |  |  | // tokenStore is an implementation of oauth2.TokenStore, which uses our db interface as a storage backend. | 
					
						
							|  |  |  | type tokenStore struct { | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	oauth2.TokenStore | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	state         *state.State | 
					
						
							|  |  |  | 	lastUsedLocks mutexes.MutexMap | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-22 22:26:54 +01:00
										 |  |  | // newTokenStore returns a token store that satisfies the oauth2.TokenStore interface. | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | // | 
					
						
							| 
									
										
										
										
											2021-03-22 22:26:54 +01:00
										 |  |  | // In order to allow tokens to 'expire', it will also set off a goroutine that iterates through | 
					
						
							|  |  |  | // the tokens in the DB once per minute and deletes any that have expired. | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | func newTokenStore(ctx context.Context, state *state.State) oauth2.TokenStore { | 
					
						
							|  |  |  | 	ts := &tokenStore{state: state} | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	// Set the token store to clean out expired tokens | 
					
						
							|  |  |  | 	// once per minute, or return if we're done. | 
					
						
							| 
									
										
										
										
											2021-10-11 05:37:33 -07:00
										 |  |  | 	go func(ctx context.Context, ts *tokenStore) { | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	cleanloop: | 
					
						
							|  |  |  | 		for { | 
					
						
							|  |  |  | 			select { | 
					
						
							|  |  |  | 			case <-ctx.Done(): | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 				log.Info(ctx, "breaking cleanloop") | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 				break cleanloop | 
					
						
							|  |  |  | 			case <-time.After(1 * time.Minute): | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 				log.Trace(ctx, "sweeping out old oauth entries broom broom") | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 				if err := ts.sweep(ctx); err != nil { | 
					
						
							| 
									
										
										
										
											2023-02-17 12:02:29 +01:00
										 |  |  | 					log.Errorf(ctx, "error while sweeping oauth entries: %s", err) | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-10-11 05:37:33 -07:00
										 |  |  | 	}(ctx, ts) | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	return ts | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | // sweep clears out old tokens that have expired; | 
					
						
							|  |  |  | // it should be run on a loop about once per minute or so. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func (ts *tokenStore) sweep(ctx context.Context) error { | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	// Select *all* tokens from the db | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// TODO: if this becomes expensive | 
					
						
							|  |  |  | 	// (ie., there are fucking LOADS of | 
					
						
							|  |  |  | 	// tokens) then figure out a better way. | 
					
						
							|  |  |  | 	tokens, err := ts.state.DB.GetAllTokens(ctx) | 
					
						
							| 
									
										
										
										
											2024-04-15 14:22:21 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	// Remove any expired tokens, bearing | 
					
						
							|  |  |  | 	// in mind that zero time = no expiry. | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	now := time.Now() | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	for _, token := range tokens { | 
					
						
							|  |  |  | 		var expired bool | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch { | 
					
						
							|  |  |  | 		case !token.CodeExpiresAt.IsZero() && token.CodeExpiresAt.Before(now): | 
					
						
							|  |  |  | 			log.Tracef(ctx, "code token %s is expired", token.ID) | 
					
						
							|  |  |  | 			expired = true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case !token.RefreshExpiresAt.IsZero() && token.RefreshExpiresAt.Before(now): | 
					
						
							|  |  |  | 			log.Tracef(ctx, "refresh token %s is expired", token.ID) | 
					
						
							|  |  |  | 			expired = true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case !token.AccessExpiresAt.IsZero() && token.AccessExpiresAt.Before(now): | 
					
						
							|  |  |  | 			log.Tracef(ctx, "access token %s is expired", token.ID) | 
					
						
							|  |  |  | 			expired = true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !expired { | 
					
						
							|  |  |  | 			// Token's | 
					
						
							|  |  |  | 			// still good. | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err := ts.state.DB.DeleteTokenByID(ctx, token.ID); err != nil { | 
					
						
							|  |  |  | 			err := gtserror.Newf("db error expiring token %s: %w", token.ID, err) | 
					
						
							|  |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Create creates and store the new token information. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func (ts *tokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error { | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	t, ok := info.(*models.Token) | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		return errors.New("info param was not a models.Token") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	dbt := TokenToDBToken(t) | 
					
						
							|  |  |  | 	if dbt.ID == "" { | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 		dbt.ID = id.NewULID() | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	return ts.state.DB.PutToken(ctx, dbt) | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RemoveByCode deletes a token from the DB based on the Code field | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func (ts *tokenStore) RemoveByCode(ctx context.Context, code string) error { | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	return ts.state.DB.DeleteTokenByCode(ctx, code) | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RemoveByAccess deletes a token from the DB based on the Access field | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func (ts *tokenStore) RemoveByAccess(ctx context.Context, access string) error { | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	return ts.state.DB.DeleteTokenByAccess(ctx, access) | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RemoveByRefresh deletes a token from the DB based on the Refresh field | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | func (ts *tokenStore) RemoveByRefresh(ctx context.Context, refresh string) error { | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 	return ts.state.DB.DeleteTokenByRefresh(ctx, refresh) | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | // GetByCode selects a token from | 
					
						
							|  |  |  | // the DB based on the Code field | 
					
						
							|  |  |  | func (ts *tokenStore) GetByCode( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	code string, | 
					
						
							|  |  |  | ) (oauth2.TokenInfo, error) { | 
					
						
							|  |  |  | 	return ts.getUpdateToken( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		ts.state.DB.GetTokenByCode, | 
					
						
							|  |  |  | 		code, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | // GetByAccess selects a token from | 
					
						
							|  |  |  | // the DB based on the Access field. | 
					
						
							|  |  |  | func (ts *tokenStore) GetByAccess( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	access string, | 
					
						
							|  |  |  | ) (oauth2.TokenInfo, error) { | 
					
						
							|  |  |  | 	return ts.getUpdateToken( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		ts.state.DB.GetTokenByAccess, | 
					
						
							|  |  |  | 		access, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | // GetByRefresh selects a token from | 
					
						
							|  |  |  | // the DB based on the Refresh field | 
					
						
							|  |  |  | func (ts *tokenStore) GetByRefresh( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	refresh string, | 
					
						
							|  |  |  | ) (oauth2.TokenInfo, error) { | 
					
						
							|  |  |  | 	return ts.getUpdateToken( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		ts.state.DB.GetTokenByRefresh, | 
					
						
							|  |  |  | 		refresh, | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // package-internal function for getting a token | 
					
						
							|  |  |  | // and potentially updating its last_used value. | 
					
						
							|  |  |  | func (ts *tokenStore) getUpdateToken( | 
					
						
							|  |  |  | 	ctx context.Context, | 
					
						
							|  |  |  | 	getBy func(context.Context, string) (*gtsmodel.Token, error), | 
					
						
							|  |  |  | 	key string, | 
					
						
							|  |  |  | ) (oauth2.TokenInfo, error) { | 
					
						
							|  |  |  | 	// Hold a lock to get the token based on | 
					
						
							|  |  |  | 	// whatever func + key we've been given. | 
					
						
							|  |  |  | 	unlock := ts.lastUsedLocks.Lock(key) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	token, err := getBy(ctx, key) | 
					
						
							| 
									
										
										
										
											2024-04-15 14:22:21 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 		// Unlock on error. | 
					
						
							|  |  |  | 		unlock() | 
					
						
							| 
									
										
										
										
											2021-03-22 22:26:54 +01:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-03-03 16:03:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// If token was last used more than | 
					
						
							|  |  |  | 	// an hour ago, update this in the db. | 
					
						
							|  |  |  | 	wasLastUsed := token.LastUsed | 
					
						
							|  |  |  | 	if now := time.Now(); now.Sub(wasLastUsed) > 1*time.Hour { | 
					
						
							|  |  |  | 		token.LastUsed = now | 
					
						
							|  |  |  | 		if err := ts.state.DB.UpdateToken(ctx, token, "last_used"); err != nil { | 
					
						
							|  |  |  | 			// Unlock on error. | 
					
						
							|  |  |  | 			unlock() | 
					
						
							|  |  |  | 			err := gtserror.Newf("error updating last_used on token: %w", err) | 
					
						
							|  |  |  | 			return nil, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// We're done, unlock. | 
					
						
							|  |  |  | 	unlock() | 
					
						
							| 
									
										
										
										
											2024-04-15 14:22:21 +01:00
										 |  |  | 	return DBTokenToToken(token), nil | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	The following models are basically helpers for the token store implementation, they should only be used internally. | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | // TokenToDBToken is a lil util function that takes a gotosocial token and gives back a token for inserting into a database. | 
					
						
							| 
									
										
										
										
											2021-09-01 11:45:01 +02:00
										 |  |  | func TokenToDBToken(tkn *models.Token) *gtsmodel.Token { | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	now := time.Now() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// For the following, we want to make sure we're not adding a time.Now() to an *empty* ExpiresIn, otherwise that's | 
					
						
							|  |  |  | 	// going to cause all sorts of interesting problems. So check first to make sure that the ExpiresIn is not equal | 
					
						
							|  |  |  | 	// to the zero value of a time.Duration, which is 0s. If it *is* empty/nil, just leave the ExpiresAt at nil as well. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 	cea := time.Time{} | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	if tkn.CodeExpiresIn != 0*time.Second { | 
					
						
							|  |  |  | 		cea = now.Add(tkn.CodeExpiresIn) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 	aea := time.Time{} | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	if tkn.AccessExpiresIn != 0*time.Second { | 
					
						
							|  |  |  | 		aea = now.Add(tkn.AccessExpiresIn) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 	rea := time.Time{} | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	if tkn.RefreshExpiresIn != 0*time.Second { | 
					
						
							|  |  |  | 		rea = now.Add(tkn.RefreshExpiresIn) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-01 11:45:01 +02:00
										 |  |  | 	return >smodel.Token{ | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 		ClientID:            tkn.ClientID, | 
					
						
							|  |  |  | 		UserID:              tkn.UserID, | 
					
						
							|  |  |  | 		RedirectURI:         tkn.RedirectURI, | 
					
						
							|  |  |  | 		Scope:               tkn.Scope, | 
					
						
							|  |  |  | 		Code:                tkn.Code, | 
					
						
							|  |  |  | 		CodeChallenge:       tkn.CodeChallenge, | 
					
						
							|  |  |  | 		CodeChallengeMethod: tkn.CodeChallengeMethod, | 
					
						
							|  |  |  | 		CodeCreateAt:        tkn.CodeCreateAt, | 
					
						
							|  |  |  | 		CodeExpiresAt:       cea, | 
					
						
							|  |  |  | 		Access:              tkn.Access, | 
					
						
							|  |  |  | 		AccessCreateAt:      tkn.AccessCreateAt, | 
					
						
							|  |  |  | 		AccessExpiresAt:     aea, | 
					
						
							|  |  |  | 		Refresh:             tkn.Refresh, | 
					
						
							|  |  |  | 		RefreshCreateAt:     tkn.RefreshCreateAt, | 
					
						
							|  |  |  | 		RefreshExpiresAt:    rea, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | // DBTokenToToken is a lil util function that takes a database token and gives back a gotosocial token | 
					
						
							| 
									
										
										
										
											2021-09-01 11:45:01 +02:00
										 |  |  | func DBTokenToToken(dbt *gtsmodel.Token) *models.Token { | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	now := time.Now() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 	var codeExpiresIn time.Duration | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	if !dbt.CodeExpiresAt.IsZero() { | 
					
						
							|  |  |  | 		codeExpiresIn = dbt.CodeExpiresAt.Sub(now) | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var accessExpiresIn time.Duration | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	if !dbt.AccessExpiresAt.IsZero() { | 
					
						
							|  |  |  | 		accessExpiresIn = dbt.AccessExpiresAt.Sub(now) | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var refreshExpiresIn time.Duration | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	if !dbt.RefreshExpiresAt.IsZero() { | 
					
						
							|  |  |  | 		refreshExpiresIn = dbt.RefreshExpiresAt.Sub(now) | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	return &models.Token{ | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 		ClientID:            dbt.ClientID, | 
					
						
							|  |  |  | 		UserID:              dbt.UserID, | 
					
						
							|  |  |  | 		RedirectURI:         dbt.RedirectURI, | 
					
						
							|  |  |  | 		Scope:               dbt.Scope, | 
					
						
							|  |  |  | 		Code:                dbt.Code, | 
					
						
							|  |  |  | 		CodeChallenge:       dbt.CodeChallenge, | 
					
						
							|  |  |  | 		CodeChallengeMethod: dbt.CodeChallengeMethod, | 
					
						
							|  |  |  | 		CodeCreateAt:        dbt.CodeCreateAt, | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 		CodeExpiresIn:       codeExpiresIn, | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 		Access:              dbt.Access, | 
					
						
							|  |  |  | 		AccessCreateAt:      dbt.AccessCreateAt, | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 		AccessExpiresIn:     accessExpiresIn, | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 		Refresh:             dbt.Refresh, | 
					
						
							|  |  |  | 		RefreshCreateAt:     dbt.RefreshCreateAt, | 
					
						
							| 
									
										
										
										
											2021-05-10 16:29:05 +02:00
										 |  |  | 		RefreshExpiresIn:    refreshExpiresIn, | 
					
						
							| 
									
										
										
										
											2021-03-14 17:56:16 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |