Documentation
¶
Overview ¶
Package msgraph serves mounts over folders in SharePoint document libraries and OneDrives, through Microsoft Graph's REST API. A source names a drive and a folder in it:
msgraph://sites/<site-id>/drives/<drive-id>/root/<folder> msgraph://drives/<drive-id>/root/<folder> msgraph://me/drive/root/<folder>
It is its own module, like the S3 and GCS drivers, though it uses plain HTTP and no Graph SDK. The listing, pinning and fs.FS logic is in storage/mounts/bucket, and this package adapts a drive to its Bucket interface, with the part of the source before the first "/" as the bucket name.
A drive has no change that spans several files, so a snapshot's version is the hash of a pinned listing, as for a bucket. Each file is pinned by its item id, its content tag (cTag, or eTag where Graph sends no cTag), its size and its last-modified time. A pinned read serves the item while its content tag still matches. After a change it serves the entry in the item's version history with the pinned modified time and size. When the library keeps no such version, the read fails with the path named and mounts.ErrConflict.
Delta asks Graph what changed since a token, so a poll reads only the changes. Subscriptions are the push form: CreateSubscription and RenewSubscription manage one, AnswerValidation answers Graph's validation handshake, and ParseNotification turns a notification into a mounts.Change.
The driver signs with mounts.BearerCredentials, which a connections KindEntra or KindEntraUser connection gives. Any other kind returns mounts.ErrInvalidArgument.
Index ¶
- Constants
- Variables
- func AnswerValidation(w http.ResponseWriter, r *http.Request) bool
- func ParseNotification(r *http.Request, clientState string) ([]mounts.Change, error)
- type DeltaItem
- type DeltaPage
- type Driver
- func (d *Driver) CanRead(ctx context.Context, baseURL, source string, creds mounts.Credentials) (bool, error)
- func (d *Driver) ContainsSource(mountSource, changed string) bool
- func (d *Driver) CreateSubscription(ctx context.Context, m mounts.Mount, creds mounts.Credentials, ...) (Subscription, error)
- func (d *Driver) DeleteSubscription(ctx context.Context, creds mounts.Credentials, id string) error
- func (d *Driver) Delta(ctx context.Context, m mounts.Mount, creds mounts.Credentials, token string) (DeltaPage, error)
- func (d *Driver) RenewSubscription(ctx context.Context, creds mounts.Credentials, id string, expires time.Time) (Subscription, error)
- type Option
- type Subscription
Constants ¶
const DefaultBaseURL = "https://graph.microsoft.com/v1.0"
DefaultBaseURL is Microsoft Graph's v1.0 endpoint for public Azure. WithBaseURL sets another, for a national cloud or a test's fake.
const Scheme = "msgraph"
Scheme is the scheme of a msgraph source.
Variables ¶
var ErrBadClientState = errors.New("msgraph: notification clientState doesn't match")
ErrBadClientState means a notification's clientState isn't the one the app gave CreateSubscription, so it didn't come from that subscription. The app answers 202 anyway, as Graph asks, and ignores the body.
var ErrResyncRequired = errors.New("msgraph: delta token expired, resync required")
ErrResyncRequired means Graph no longer knows a delta token, so the app has to read the mount again in full and start over with an empty token. Graph answers 410 Gone for it.
Functions ¶
func AnswerValidation ¶
func AnswerValidation(w http.ResponseWriter, r *http.Request) bool
AnswerValidation answers Graph's validation request, which carries a validationToken query parameter, by echoing the token as text/plain with 200. It reports whether r was one, so a handler calls it first and returns when it is true. The plain-text content type keeps a browser from running a crafted token as HTML.
func ParseNotification ¶
ParseNotification reads a Graph change notification and returns one mounts.Change per entry. Each Change's Source is the drive root that changed, such as "msgraph://drives/b!x/root", with Default set and no Ref, since Graph says only that something in the drive changed. The app then calls Delta on its mounts in that drive to learn which files. Every entry's clientState must equal clientState, compared in constant time, or ParseNotification returns ErrBadClientState and no changes.
Types ¶
type DeltaItem ¶
type DeltaItem struct {
// Path is the file's path inside the mount. It is empty for a deleted item whose path Graph
// didn't send, which SharePoint and OneDrive for Business do for deletions.
Path string
// ItemID is the item's Graph id, which names the file even when Path is empty.
ItemID string
// Deleted is true when the file was deleted.
Deleted bool
}
DeltaItem is one file that changed under a mount since a delta token.
type DeltaPage ¶
type DeltaPage struct {
// Items are the files under the mount that changed, in the order Graph sent them. Folders
// are left out.
Items []DeltaItem
// Token is what the next Delta call passes. The app keeps it beside the mount.
Token string
}
DeltaPage is what one Delta call found.
type Driver ¶
Driver serves mounts.KindMSGraph mounts. The embedded bucket.Driver implements mounts.Driver, mounts.Preparer, mounts.VersionReader and mounts.Localizer. Driver adds Delta and the subscription calls.
func (*Driver) CanRead ¶ added in v0.7.31
func (d *Driver) CanRead(ctx context.Context, baseURL, source string, creds mounts.Credentials) (bool, error)
CanRead reports whether creds can read the folder at source, such as "msgraph://sites/<site>/drives/<drive>/root/Boards". It fits connections.ReadCheck, so an app passes it to connections.WithReadCheckFor("msgraph", d.CanRead, connections.KindEntraUser), and a person's own Entra sign-in decides whether they see a SharePoint or OneDrive mount.
It asks Graph for the folder's item with one GET. A 403 or a 404 is a definite no: Graph answers 403 for a library the person can't open, and 404 for a drive or folder they can't see or that doesn't exist. A rejected token (401), throttling and any other failure are errors, since they say nothing about access.
baseURL is ignored. For an Entra connection it is the sign-in authority rather than Graph, and the driver's own Graph root (WithBaseURL) says where to ask.
func (*Driver) ContainsSource ¶
ContainsSource implements mounts.SourceContainer, so Registry.Following finds the mounts a notification is about. Graph notifies per drive, and ParseNotification names the drive root, such as "msgraph://drives/<id>/root". So a change contains a mount when the mount's folder is in that drive, the reverse of the bucket rule it would otherwise inherit, where a mount contains an object under its prefix. A change naming a file, as a caller might build from Delta, still finds the mount holding it.
func (*Driver) CreateSubscription ¶
func (d *Driver) CreateSubscription(ctx context.Context, m mounts.Mount, creds mounts.Credentials, notificationURL, clientState string, expires time.Time) (Subscription, error)
CreateSubscription asks Graph to POST to notificationURL whenever something in m's drive changes. Graph first sends notificationURL a validation request, which the app's handler answers with AnswerValidation, so the handler must be serving before this call. clientState is a secret the app picks and later passes to ParseNotification. Graph caps a drive subscription at about 30 days, and the app renews it with RenewSubscription before expires. Graph watches a whole drive, not one folder.
func (*Driver) DeleteSubscription ¶
DeleteSubscription ends subscription id. One that is already gone isn't an error.
func (*Driver) Delta ¶
func (d *Driver) Delta(ctx context.Context, m mounts.Mount, creds mounts.Credentials, token string) (DeltaPage, error)
Delta returns the files under m's folder that changed since token, and the token for the next call. An empty token returns no items and a token for "now", so an app starts following a mount from the moment it is made. Graph tracks changes for a whole drive, so Delta asks for the drive's changes and keeps those under m's folder, along with deletions whose path Graph left out. A token Graph has forgotten returns ErrResyncRequired; the app then reads the mount in full and starts over with an empty token.
func (*Driver) RenewSubscription ¶
func (d *Driver) RenewSubscription(ctx context.Context, creds mounts.Credentials, id string, expires time.Time) (Subscription, error)
RenewSubscription moves subscription id's expiry to expires.
type Option ¶
type Option func(*options)
Option configures a Driver.
func WithBaseURL ¶
WithBaseURL points the driver at another Graph root than DefaultBaseURL, such as a national cloud's ("https://graph.microsoft.us/v1.0") or a test's fake.
func WithHTTPClient ¶
WithHTTPClient sets the client for every request. The default is http.DefaultClient.
func WithLimits ¶
WithLimits caps how many files and bytes a source may hold. A zero field keeps mounts.DefaultLimits' value.
type Subscription ¶
type Subscription struct {
ID string `json:"id"`
Resource string `json:"resource"`
NotificationURL string `json:"notificationUrl"`
ClientState string `json:"clientState"`
Expires time.Time `json:"expirationDateTime"`
}
Subscription is a Graph change-notification subscription on a drive.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package graphtest is a fake Microsoft Graph for tests of code that reads SharePoint and OneDrive drives: the msgraph mount driver, and an app's own tests.
|
Package graphtest is a fake Microsoft Graph for tests of code that reads SharePoint and OneDrive drives: the msgraph mount driver, and an app's own tests. |