| 1 |
// Copyright (c) 2017 VMware, Inc. All Rights Reserved. |
| 2 |
// |
| 3 |
// This product is licensed to you under the Apache License, Version 2.0 (the "License"). |
| 4 |
// You may not use this product except in compliance with the License. |
| 5 |
// |
| 6 |
// This product may include a number of subcomponents with separate copyright notices and |
| 7 |
// license terms. Your use of these subcomponents is subject to the terms and conditions |
| 8 |
// of the subcomponent's license, as noted in the LICENSE file. |
| 9 |
|
| 10 |
// +build windows |
| 11 |
|
| 12 |
package lightwave |
| 13 |
|
| 14 |
import ( |
| 15 |
"encoding/base64" |
| 16 |
"fmt" |
| 17 |
"github.com/vmware/photon-controller-go-sdk/SSPI" |
| 18 |
"math/rand" |
| 19 |
"net" |
| 20 |
"net/url" |
| 21 |
"strings" |
| 22 |
"time" |
| 23 |
) |
| 24 |
|
| 25 |
const gssTicketGrantFormatString = "grant_type=urn:vmware:grant_type:gss_ticket&gss_ticket=%s&context_id=%s&scope=%s" |
| 26 |
|
| 27 |
// GetTokensFromWindowsLogInContext gets tokens based on Windows logged in context |
| 28 |
// Here is how it works: |
| 29 |
// 1. Get the SPN (Service Principal Name) in the format host/FQDN of lightwave. This is needed for SSPI/Kerberos protocol |
| 30 |
// 2. Call Windows API AcquireCredentialsHandle() using SSPI library. This will give the current users credential handle |
| 31 |
// 3. Using this handle call Windows API AcquireCredentialsHandle(). This will give you byte[] |
| 32 |
// 4. Encode this byte[] and send it to OIDC server over HTTP (using POST) |
| 33 |
// 5. OIDC server can send either of the following |
| 34 |
// - Access tokens. In this case return access tokens to client |
| 35 |
// - Error in the format: invalid_grant: gss_continue_needed:'context id':'token from server' |
| 36 |
// 6. In case you get error, parse it and get the token from server |
| 37 |
// 7. Feed this token to step 3 and repeat steps till you get the access tokens from server |
| 38 |
func (client *OIDCClient) GetTokensFromWindowsLogInContext() (tokens *OIDCTokenResponse, err error) { |
| 39 |
spn, err := client.buildSPN() |
| 40 |
if err != nil { |
| 41 |
return nil, err |
| 42 |
} |
| 43 |
|
| 44 |
auth, _ := SSPI.GetAuth("", "", spn, "") |
| 45 |
|
| 46 |
userContext, err := auth.InitialBytes() |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
|
| 51 |
// In case of multiple req/res between client and server (as explained in above comment), |
| 52 |
// server needs to maintain the mapping of context id -> token |
| 53 |
// So we need to generate random string as a context id |
| 54 |
// If we use same context id for all the requests, results can be erroneous |
| 55 |
contextId := client.generateRandomString() |
| 56 |
body := fmt.Sprintf(gssTicketGrantFormatString, url.QueryEscape(base64.StdEncoding.EncodeToString(userContext)), contextId, client.Options.TokenScope) |
| 57 |
tokens, err = client.getToken(body) |
| 58 |
|
| 59 |
for { |
| 60 |
if err == nil { |
| 61 |
break |
| 62 |
} |
| 63 |
|
| 64 |
// In case of error the response will be in format: invalid_grant: gss_continue_needed:'context id':'token from server' |
| 65 |
gssToken := client.validateAndExtractGSSResponse(err, contextId) |
| 66 |
if gssToken == "" { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
|
| 70 |
data, err := base64.StdEncoding.DecodeString(gssToken) |
| 71 |
if err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
|
| 75 |
userContext, err := auth.NextBytes(data) |
| 76 |
body := fmt.Sprintf(gssTicketGrantFormatString, url.QueryEscape(base64.StdEncoding.EncodeToString(userContext)), contextId, client.Options.TokenScope) |
| 77 |
tokens, err = client.getToken(body) |
| 78 |
} |
| 79 |
|
| 80 |
return tokens, err |
| 81 |
} |
| 82 |
|
| 83 |
// Gets the SPN (Service Principal Name) in the format host/FQDN of lightwave |
| 84 |
func (client *OIDCClient) buildSPN() (spn string, err error) { |
| 85 |
u, err := url.Parse(client.Endpoint) |
| 86 |
if err != nil { |
| 87 |
return "", err |
| 88 |
} |
| 89 |
|
| 90 |
host, _, err := net.SplitHostPort(u.Host) |
| 91 |
if err != nil { |
| 92 |
return "", err |
| 93 |
} |
| 94 |
|
| 95 |
addr, err := net.LookupAddr(host) |
| 96 |
if err != nil { |
| 97 |
return "", err |
| 98 |
} |
| 99 |
|
| 100 |
var s = strings.TrimSuffix(addr[0], ".") |
| 101 |
return "host/" + s, nil |
| 102 |
} |
| 103 |
|
| 104 |
// validateAndExtractGSSResponse parse the error from server and returns token from server |
| 105 |
// In case of error from the server, response will be in format: invalid_grant: gss_continue_needed:'context id':'token from server' |
| 106 |
// So, we check for the above format in error and then return the token from server |
| 107 |
// If error is not in above format, we return empty string |
| 108 |
func (client *OIDCClient) validateAndExtractGSSResponse(err error, contextId string) string { |
| 109 |
parts := strings.Split(err.Error(), ":") |
| 110 |
if !(len(parts) == 4 && strings.TrimSpace(parts[1]) == "gss_continue_needed" && parts[2] == contextId) { |
| 111 |
return "" |
| 112 |
} else { |
| 113 |
return parts[3] |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func (client *OIDCClient) generateRandomString() string { |
| 118 |
const length = 10 |
| 119 |
const asciiA = 65 |
| 120 |
const asciiZ = 90 |
| 121 |
rand.Seed(time.Now().UTC().UnixNano()) |
| 122 |
bytes := make([]byte, length) |
| 123 |
for i := 0; i < length; i++ { |
| 124 |
bytes[i] = byte(randInt(asciiA, asciiZ)) |
| 125 |
} |
| 126 |
return string(bytes) |
| 127 |
} |
| 128 |
|
| 129 |
func randInt(min int, max int) int { |
| 130 |
return min + rand.Intn(max-min) |
| 131 |
} |