When creating a Connected App in Salesforce.com, it is possible to configure the app to "Include Custom Attributes" in the OAuth Id Token. This is found (under Lightning this is found through Setup -> PLATFORM TOOLS: Apps -> App Manager, then either click on the "New Connected App" button on the top right or click on the dropdown button on the row of an existing Connected App and select "Edit").
A description is at help.salesforce.com at the relative link articleView?id=connected_app_create.htm&type=0. Note that it's necessary to select the "Include ID Token" checkbox in order for the "Include Custom Attributes" checkbox to become available.
To define what the attributes are, traverse to "Manage" the Connected App (through Setup -> PLATFORM TOOLS: Apps -> App Manager, then either click on the dropdown button on the row of an existing Connected App and select "Manage"). There's a related list of "Custom Attributes" at the bottom of the page, where new attributes can be created. This is explained at https://help.salesforce.com/articleView?err=1&id=connected_app_view_details.htm&siteLang=en_US&type=0 with the gloriously illuminating statement: "Click New in Service Provider SAML Attributes to create attribute key/value pairs. You can also edit or delete existing attributes."
There is also information at https://help.salesforce.com/articleView?err=1&id=connected_app_manage_edit.htm&siteLang=en_US&type=0 which states: "Custom attributes are available for all connected apps. Developers can set custom SAML metadata or custom OAuth attributes for a connected app. Salesforce admins can delete or edit the attributes or add custom attributes. Attributes deleted, edited, or added by admins override attributes set by developers."
The question is, having done this and defined a Custom Attribute, how does one access the value from within apex code?
For example, if the Connected App credentials are used to perform authentication (of a user, to obtain a token) and make a call (using the token), say on a REST interface, is it possible within the REST handler to obtain the value of a Custom Attribute? If so, how?