func RevokeClientAccess(clientIID, userIID uint) { dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession. Exec("UPDATE sessions SET invalidated = true, updated_at = now() "+ "WHERE token_type IN ('access_token', 'refresh_token') AND invalidated = false AND "+ "client_id = ? AND user_id = ?;", clientIID, userIID) }
func FindClientByUUID(uuid string) models.Client { var client models.Client dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession.Where("uuid = ?", uuid).First(&client) return client }
func FindClientByKey(key string) models.Client { var client models.Client dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession.Where("key = ?", key).First(&client) return client }
func FindOrCreateLanguage(name, isoCode string) models.Language { var language models.Language dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession.Where("name = ? AND iso_code = ?", name, isoCode).First(&language) if dataStoreSession.NewRecord(language) { language = models.Language{Name: name, IsoCode: isoCode} dataStoreSession.Create(&language) } return language }
func ActiveClientsForUser(userIID uint) []models.Client { var clients []models.Client dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession. Raw("SELECT DISTINCT clients.uuid, clients.name, clients.description, clients.canonical_uri "+ "FROM clients JOIN sessions ON clients.id = sessions.client_id "+ "WHERE sessions.token_type IN ('access_token', 'refresh_token') AND sessions.invalidated = false AND "+ "sessions.user_id = ?;", userIID). Scan(&clients) return clients }
func ActiveSessionsForClient(clientIID, userIID uint) int64 { var count struct { Count int64 } dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession. Raw("SELECT count(*) AS count "+ "FROM sessions WHERE token_type IN ('access_token', 'refresh_token') AND invalidated = false AND "+ "client_id = ? AND user_id = ?;", clientIID, userIID). Scan(&count) return count.Count }
func CreateNewClient(name, description, secret, scopes, canonicalURI, redirectURI string) models.Client { var client models.Client = models.Client{ Name: name, Description: description, Secret: secret, Scopes: scopes, CanonicalURI: canonicalURI, RedirectURI: redirectURI, Type: models.ConfidentialClient, } dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession.Create(&client) return client }
func CreateSession(user models.User, client models.Client, ip, userAgent, scopes, tokenType string) models.Session { var session models.Session = models.Session{ User: user, Client: client, Ip: ip, UserAgent: userAgent, Scopes: scopes, TokenType: tokenType, } dataStore := datastore.GetDataStoreConnection() result := dataStore.Create(&session) if count := result.RowsAffected; count > 0 { return session } return models.Session{} }
func FindSessionByToken(token, tokenType string) models.Session { var session models.Session dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession. Preload("Client"). Preload("User"). Preload("User.Client"). Preload("User.Language"). Where("token = ? AND token_type = ? AND invalidated = false", token, tokenType). First(&session) if session.ID != 0 { if !session.WithinExpirationWindow() { InvalidateSession(session) return models.Session{} } } return session }
func FindOrCreateClient(name string) models.Client { var client models.Client dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession.Where("name = ?", name).First(&client) if dataStoreSession.NewRecord(client) { client = models.Client{ Name: name, Secret: models.GenerateRandomString(64), CanonicalURI: "localhost", RedirectURI: "/", Scopes: models.PublicScope, Type: models.PublicClient, } dataStoreSession.Create(&client) } return client }
func InvalidateSession(session models.Session) { dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession.Model(&session).Select("invalidated").Update("invalidated", true) }
func FindUserByAccountHolder(holder string) models.User { var user models.User dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession.Preload("Client").Preload("Language").Where("username = ? OR email = ?", holder, holder).First(&user) return user }
func FindUserByUUID(uuid string) models.User { var user models.User dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession.Preload("Client").Preload("Language").Where("uuid = ?", uuid).First(&user) return user }
func FindUserByPublicId(publicId string) models.User { var user models.User dataStoreSession := datastore.GetDataStoreConnection() dataStoreSession.Preload("Client").Preload("Language").Where("public_id = ?", publicId).First(&user) return user }