Skip to content

Commit 375f5c6

Browse files
committed
Tidy up
1 parent 1c66cf1 commit 375f5c6

File tree

13 files changed

+199
-66
lines changed

13 files changed

+199
-66
lines changed

.cfignore

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# These folders are ignored when pushing the console to CF
2-
node_modules/
32
bower_components/
4-
dist/
53
components/*/backend/vendor
4+
dev-certs/
5+
dist/
6+
docs/
7+
node_modules/
8+
out/
9+
outputs/
10+
tmp/
11+
tools/

components/app-core/backend/auth.go

+29-29
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ func (p *portalProxy) DoLoginToCNSI(c echo.Context, cnsiGUID string) (*interface
169169
}
170170
u.UserGUID = userID
171171

172-
p.saveCNSIToken(cnsiGUID, *u, uaaRes.AccessToken, uaaRes.RefreshToken)
172+
fmt.Println("DoLoginToCNSI LOGGING IN!!!!")
173+
174+
p.saveCNSIToken(cnsiGUID, *u, uaaRes.AccessToken, uaaRes.RefreshToken, false)
173175

174176
cfAdmin := strings.Contains(uaaRes.Scope, p.Config.CFAdminIdentifier)
175177

@@ -259,41 +261,38 @@ func (p *portalProxy) logoutOfCNSI(c echo.Context) error {
259261
}
260262

261263
userGUID, err := p.GetSessionStringValue(c, "user_id")
262-
fmt.Println("logoutOfCNSI: : userGUID", userGUID)
263264
if err != nil {
264-
return echo.NewHTTPError(http.StatusUnauthorized, "Could not find correct session value")
265+
return fmt.Errorf("Could not find correct session value: %s", err)
265266
}
266267

267-
userTokenInfo := userTokenInfo{
268-
UserGUID: userGUID,
268+
cnsiRecord, err := p.GetCNSIRecord(cnsiGUID)
269+
if err != nil {
270+
return fmt.Errorf("Unable to load CNSI record: %s", err)
269271
}
270272

271-
//TODO: CLOUD FOUNDRY SPECFICI logout (?) and conditional on if endpoint == CORE CF
272-
p.saveCNSIToken(cnsiGUID, userTokenInfo, "", "")
273-
//p.deleteCNSIToken(cnsiGUID, userID)
274-
275-
276-
277-
//p.GetCNSITokenRecord(cnsiGUID, userID)
278-
//_, ok := p.GetCNSITokenRecord(cnsiGUID, userID)
279-
//if !ok {
280-
// return interfaces.NewHTTPShadowError(
281-
// http.StatusBadRequest,
282-
// "Missing CNSI token, unable to log out",
283-
// "Attempt to delete a cnsi token that does not exist")
284-
//}
285-
//cfTokenRecord = interfaces.TokenRecord{}
286-
287-
//cnsiID string, u userTokenInfo, authTok string, refreshTok string
273+
// If cnsi is cf AND cf is auto-register only clear the entry
274+
if cnsiRecord.CNSIType == "cf" && p.GetConfig().AutoRegisterCFUrl == cnsiRecord.APIEndpoint.String() {
275+
log.Info("Setting token record as disconnected")
288276

289-
//type userTokenInfo struct {
290-
// UserGUID string `json:"user_id"`
291-
// UserName string `json:"user_name"`
292-
// TokenExpiry int64 `json:"exp"`
293-
// Scope []string `json:"scope"`
294-
//}
277+
cfTokenRecord, ok := p.GetCNSITokenRecord(cnsiGUID, userGUID)
278+
if !ok {
279+
return fmt.Errorf("Unable to retrieve CNSI token record: %s", err)
280+
}
295281

282+
userTokenInfo := userTokenInfo{
283+
UserGUID: userGUID,
284+
TokenExpiry: cfTokenRecord.TokenExpiry,
285+
}
296286

287+
if _, err := p.saveCNSIToken(cnsiGUID, userTokenInfo, cfTokenRecord.AuthToken, cfTokenRecord.RefreshToken, true); err != nil {
288+
return fmt.Errorf("Unable to clear token: %s", err)
289+
}
290+
} else {
291+
log.Info("Deleting Token")
292+
if err := p.deleteCNSIToken(cnsiGUID, userGUID); err != nil {
293+
return fmt.Errorf("Unable to delete token: %s", err)
294+
}
295+
}
297296

298297
return nil
299298
}
@@ -431,12 +430,13 @@ func (p *portalProxy) saveUAAToken(u userTokenInfo, authTok string, refreshTok s
431430
return tokenRecord, nil
432431
}
433432

434-
func (p *portalProxy) saveCNSIToken(cnsiID string, u userTokenInfo, authTok string, refreshTok string) (interfaces.TokenRecord, error) {
433+
func (p *portalProxy) saveCNSIToken(cnsiID string, u userTokenInfo, authTok string, refreshTok string, disconnect bool) (interfaces.TokenRecord, error) {
435434
log.Debug("saveCNSIToken")
436435
tokenRecord := interfaces.TokenRecord{
437436
AuthToken: authTok,
438437
RefreshToken: refreshTok,
439438
TokenExpiry: u.TokenExpiry,
439+
Disconnected: disconnect,
440440
}
441441

442442
err := p.setCNSITokenRecord(cnsiID, u.UserGUID, tokenRecord)

components/app-core/backend/cnsi.go

+34
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,21 @@ func (p *portalProxy) GetCNSITokenRecord(cnsiGUID string, userGUID string) (inte
342342
return tr, true
343343
}
344344

345+
func (p *portalProxy) GetCNSITokenRecordWithDisconnected(cnsiGUID string, userGUID string) (interfaces.TokenRecord, bool) {
346+
log.Debug("GetCNSITokenRecord")
347+
tokenRepo, err := tokens.NewPgsqlTokenRepository(p.DatabaseConnectionPool)
348+
if err != nil {
349+
return interfaces.TokenRecord{}, false
350+
}
351+
352+
tr, err := tokenRepo.FindCNSIToken(cnsiGUID, userGUID, p.Config.EncryptionKeyInBytes)
353+
if err != nil {
354+
return interfaces.TokenRecord{}, false
355+
}
356+
357+
return tr, true
358+
}
359+
345360
//TODO: remove this? It is unusable in this form as we won't know for which CNSI each token is
346361
func (p *portalProxy) listCNSITokenRecordsForUser(userGUID string) ([]*interfaces.TokenRecord, error) {
347362
log.Debug("listCNSITokenRecordsForUser")
@@ -394,3 +409,22 @@ func (p *portalProxy) unsetCNSITokenRecord(cnsiGUID string, userGUID string) err
394409

395410
return nil
396411
}
412+
413+
//func (p *portalProxy) clearCNSITokenRecord(cnsiGUID string, userGUID string) error {
414+
// log.Debug("clearCNSITokenRecord")
415+
// tokenRepo, err := tokens.NewPgsqlTokenRepository(p.DatabaseConnectionPool)
416+
// if err != nil {
417+
// msg := "Unable to establish a database reference: '%v'"
418+
// log.Errorf(msg, err)
419+
// return fmt.Errorf(msg, err)
420+
// }
421+
//
422+
// err = tokenRepo.ClearCNSIToken(cnsiGUID, userGUID, p.Config.EncryptionKeyInBytes)
423+
// if err != nil {
424+
// msg := "Unable to clear a CNSI Token: %v"
425+
// log.Errorf(msg, err)
426+
// return fmt.Errorf(msg, err)
427+
// }
428+
//
429+
// return nil
430+
//}

components/app-core/backend/oauth_requests.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (p *portalProxy) RefreshToken(skipSSLValidation bool, cnsiGUID, userGUID, c
9898

9999
u.UserGUID = userGUID
100100

101-
t, err = p.saveCNSIToken(cnsiGUID, *u, uaaRes.AccessToken, uaaRes.RefreshToken)
101+
t, err = p.saveCNSIToken(cnsiGUID, *u, uaaRes.AccessToken, uaaRes.RefreshToken, t.Disconnected)
102102
if err != nil {
103103
return t, fmt.Errorf("Couldn't save new token: %v", err)
104104
}

components/app-core/backend/repository/cnsis/pgsql_cnsis.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
var listCNSIs = `SELECT guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation
1616
FROM cnsis`
1717

18-
var listCNSIsByUser = `SELECT c.guid, c.name, c.cnsi_type, c.api_endpoint, t.user_guid, t.token_expiry, c.skip_ssl_validation
18+
var listCNSIsByUser = `SELECT c.guid, c.name, c.cnsi_type, c.api_endpoint, t.user_guid, t.token_expiry, c.skip_ssl_validation, t.disconnected
1919
FROM cnsis c, tokens t
20-
WHERE c.guid = t.cnsi_guid AND t.token_type=$1 AND t.user_guid=$2`
20+
WHERE c.guid = t.cnsi_guid AND t.token_type=$1 AND t.user_guid=$2 AND t.disconnected = 0`
2121

2222
var findCNSI = `SELECT guid, name, cnsi_type, api_endpoint, auth_endpoint, token_endpoint, doppler_logging_endpoint, skip_ssl_validation
2323
FROM cnsis
@@ -110,17 +110,21 @@ func (p *PostgresCNSIRepository) ListByUser(userGUID string) ([]*RegisteredClust
110110
var clusterList []*RegisteredCluster
111111
clusterList = make([]*RegisteredCluster, 0)
112112

113+
fmt.Println("1ListByUser")
113114
for rows.Next() {
115+
fmt.Println("2ListByUser")
114116
var (
115-
pCNSIType string
116-
pURL string
117+
pCNSIType string
118+
pURL string
119+
disconnected bool
117120
)
118121

119122
cluster := new(RegisteredCluster)
120-
err := rows.Scan(&cluster.GUID, &cluster.Name, &pCNSIType, &pURL, &cluster.Account, &cluster.TokenExpiry, &cluster.SkipSSLValidation)
123+
err := rows.Scan(&cluster.GUID, &cluster.Name, &pCNSIType, &pURL, &cluster.Account, &cluster.TokenExpiry, &cluster.SkipSSLValidation, &disconnected)
121124
if err != nil {
122125
return nil, fmt.Errorf("Unable to scan cluster records: %v", err)
123126
}
127+
fmt.Println("3ListByUser disconnected: ", disconnected)
124128

125129
cluster.CNSIType = pCNSIType
126130

components/app-core/backend/repository/interfaces/portal_proxy.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type PortalProxy interface {
3030
GetCNSIRecord(guid string) (CNSIRecord, error)
3131
GetCNSIRecordByEndpoint(endpoint string) (CNSIRecord, error)
3232
GetCNSITokenRecord(cnsiGUID string, userGUID string) (TokenRecord, bool)
33+
GetCNSITokenRecordWithDisconnected(cnsiGUID string, userGUID string) (TokenRecord, bool)
3334
GetCNSIUser(cnsiGUID string, userGUID string) (*ConnectedUser, bool)
3435
GetConfig() *PortalConfig
3536

components/app-core/backend/repository/interfaces/structs.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type TokenRecord struct {
3636
AuthToken string
3737
RefreshToken string
3838
TokenExpiry int64
39+
Disconnected bool
3940
}
4041

4142
type CFInfo struct {

0 commit comments

Comments
 (0)