Start using the ArcGIS client to get user data with the token
This commit is contained in:
parent
858bc031a0
commit
ff1fd18da1
5 changed files with 51 additions and 5 deletions
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[submodule "arcgis-go"]
|
||||
path = arcgis-go
|
||||
url = git@github.com:Gleipnir-Technology/arcgis-go.git
|
||||
1
arcgis-go
Submodule
1
arcgis-go
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit b33f892f49df816e2621394187e1c80ab395a8c6
|
||||
48
arcgis.go
48
arcgis.go
|
|
@ -8,7 +8,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
|
@ -16,6 +16,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/Gleipnir-Technology/arcgis-go"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/models"
|
||||
)
|
||||
|
||||
|
|
@ -60,6 +61,37 @@ func generateCodeVerifier() string {
|
|||
return base64.RawURLEncoding.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
// Find out what we can about this user
|
||||
func getArcgisUserData(access_token string, expires time.Time, refresh_token string) {
|
||||
client := arcgis.NewArcGIS(
|
||||
arcgis.AuthenticatorOAuth{
|
||||
AccessToken: access_token,
|
||||
Expires: expires,
|
||||
RefreshToken: refresh_token,
|
||||
},
|
||||
)
|
||||
/*portal, err := client.PortalsSelf()
|
||||
if err != nil {
|
||||
slog.Error("Failed to get ArcGIS user data", slog.String("err", err.Error()))
|
||||
return
|
||||
}
|
||||
slog.Info("Got portals data",
|
||||
slog.String("Username", portal.User.Username),
|
||||
slog.String("ID", portal.ID))
|
||||
*/
|
||||
search, err := client.Search("Fieldseeker")
|
||||
if err != nil {
|
||||
slog.Error("Failed to get search FieldseekerGIS data", slog.String("err", err.Error()))
|
||||
return
|
||||
}
|
||||
for _, result := range search.Results {
|
||||
slog.Info("Got result", slog.String("name", result.Name))
|
||||
//if result.Name == "FieldseekerGIS" {
|
||||
//slog.Info("Found Fieldseeker", slog.String("url", result.URL))
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
func handleOauthAccessCode(ctx context.Context, user *models.User, code string) error {
|
||||
baseURL := "https://www.arcgis.com/sharing/rest/oauth2/token/"
|
||||
|
||||
|
|
@ -78,14 +110,14 @@ func handleOauthAccessCode(ctx context.Context, user *models.User, code string)
|
|||
}
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
client := http.Client{}
|
||||
log.Printf("POST %s", baseURL)
|
||||
slog.Info("POST", slog.String("url", baseURL))
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to do request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
log.Printf("Response %d", resp.StatusCode)
|
||||
slog.Info("Response", slog.Int("status", resp.StatusCode))
|
||||
if resp.StatusCode >= http.StatusBadRequest {
|
||||
if err != nil {
|
||||
return fmt.Errorf("Got status code %d and failed to read response body: %v", resp.StatusCode, err)
|
||||
|
|
@ -102,11 +134,16 @@ func handleOauthAccessCode(ctx context.Context, user *models.User, code string)
|
|||
if err != nil {
|
||||
return fmt.Errorf("Failed to unmarshal JSON: %v", err)
|
||||
}
|
||||
log.Printf("Refresh token '%s'", tokenResponse.RefreshToken)
|
||||
slog.Info("Oauth token acquired",
|
||||
slog.String("refresh token", tokenResponse.RefreshToken),
|
||||
slog.String("access token", tokenResponse.AccessToken),
|
||||
slog.Int("expires", tokenResponse.ExpiresIn),
|
||||
)
|
||||
|
||||
expires := futureUTCTimestamp(tokenResponse.ExpiresIn)
|
||||
setter := models.OauthTokenSetter{
|
||||
AccessToken: omit.From(tokenResponse.AccessToken),
|
||||
Expires: omit.From(futureUTCTimestamp(tokenResponse.ExpiresIn)),
|
||||
Expires: omit.From(expires),
|
||||
RefreshToken: omit.From(tokenResponse.RefreshToken),
|
||||
Username: omit.From(tokenResponse.Username),
|
||||
}
|
||||
|
|
@ -114,6 +151,7 @@ func handleOauthAccessCode(ctx context.Context, user *models.User, code string)
|
|||
if err != nil {
|
||||
return fmt.Errorf("Failed to save token to database: %v", err)
|
||||
}
|
||||
go getArcgisUserData(tokenResponse.AccessToken, expires, tokenResponse.RefreshToken)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -3,6 +3,7 @@ module github.com/Gleipnir-Technology/nidus-sync
|
|||
go 1.24.9
|
||||
|
||||
require (
|
||||
github.com/Gleipnir-Technology/arcgis-go v0.0.2
|
||||
github.com/aarondl/opt v0.0.0-20250607033636-982744e1bd65
|
||||
github.com/alexedwards/scs/pgxstore v0.0.0-20251002162104-209de6e426de
|
||||
github.com/alexedwards/scs/v2 v2.9.0
|
||||
|
|
@ -44,3 +45,4 @@ require (
|
|||
golang.org/x/text v0.29.0 // indirect
|
||||
google.golang.org/protobuf v1.36.5 // indirect
|
||||
)
|
||||
replace github.com/Gleipnir-Technology/arcgis-go => ./arcgis-go
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -2,6 +2,8 @@ dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
|
|||
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||
github.com/Gleipnir-Technology/arcgis-go v0.0.2 h1:mmIh8M22rYUTHttX0JQ29CON99izzT43RrQbf/NX4w0=
|
||||
github.com/Gleipnir-Technology/arcgis-go v0.0.2/go.mod h1:INOPGfEriR2hcwD4lXIuQOuYigx3OuAnK5PmbKlj9bs=
|
||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||
github.com/aarondl/opt v0.0.0-20250607033636-982744e1bd65 h1:lbdPe4LBNmNDzeQFwNhEc88w90841qv737MI4+aXSYU=
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue