npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@vtfk/maskinporten-auth

v2.0.5

Published

For getting tokens from Maskinporten

Downloads

10

Readme

maskinportenAuth

For getting access tokens from maskinporten

Tar denne på norsk, siden maskinporten er norsk

Nyttige lenker

Installasjon

npm i @vtfk/maskinporten-auth

Mer spesifikke steg beskrives under

Bruk

Du må ha på plass sertifikater og nøkler før du kan bruke denne pakka. Se avsnitt Oppsett

const maskinportenToken = require('@vtfk/maskinporten-auth')
// PEM
const options = {
    url: 'url for hente token fra maskinporten', // Sjekk ulike endepunkter her: https://docs.digdir.no/docs/Maskinporten/maskinporten_func_wellknown.html
    pemcert: 'et PEM sertifikat som BASE64-string',
    pemprivateKey: 'en PEM privat nøkkel som BASE64-string',
    audience: 'https://maskinporten.no', // // Sjekk ulike audience her: https://docs.digdir.no/docs/Maskinporten/maskinporten_func_wellknown.html
    issuer: 'klientID-guid fra maskinporten klienten du har satt opp',
    scope: 'prefix:scope', // Scopet du vil ha token for
}
// Eller PFX
const optionsPfx = {
    url: 'url for hente token fra maskinporten', // Sjekk ulike endepunkter her: https://docs.digdir.no/docs/Maskinporten/maskinporten_func_wellknown.html
    pfxcert: 'et PFX sertifikat som BASE64-string',
    privateKeyPassphrase: 'krypertingspassordet for sertifikatets privatekey',
    audience: 'https://maskinporten.no', // // Sjekk ulike audience her: https://docs.digdir.no/docs/Maskinporten/maskinporten_func_wellknown.html
    issuer: 'klientID-guid fra maskinporten klienten du har satt opp',
    scope: 'prefix:scope', // Scopet du vil ha token for
}

try {
    const token = await maskinportenToken(options)
    console.log(token)
} catch (error) {
    console.log('Something went wrong. ', error)
}

Oppsett

Forutsetninger

1. Sett opp integrasjon i selvbetjeningsportalen

  • Logg på Samarbeidsportalen. Klikk deg inn på Integrasjoner -> Selvbetjening

  • Klikk deg inn på Integrasjoner->selvbetjening

  • Velg "Ver 1" for test-miljø, "Ver 2" for pre-release test-miljø (kommende funksjoner), "Produksjon" for, ja, produksjon

  • Versjoner

  • Opprett en ny integrasjon. Legg til de scopene integrasjonen skal ha tilgang til (f. eks "ks:fiks", Les mer om scopes her)

  • Resultatet bør se omtrent slik ut:

  • Integrasjon

  • Når integrasjonen er ferdig satt opp, ta med deg Integrasjons-ID, og scopes for integrasjonen (du finner de igjen i selvbetjeningsportalen, så no worries om du glemmer de)

2. Gjør klar sertifikat for å gjøre spørringer om access token

  • Om du satte opp integrasjonen i "Produksjon", bruk prod-virksomhetssertifikat, om integrasjonen er i "Ver 1" eller "Ver 2", bruk test-virksomhetetssertifikat. Organisasjonen din bør ha begge typer, om du er usikker - spør en voksen.
  • Om du har et .p12 sertifikat - ta å rename det til .pfx

2.1 PEM

2.1.1 Konvertere P12 til PEM
  • Lagre sertifikatet du skal bruke sikkert et sted du kan kjøre openssl (UNIX) (hvis du har Windows - bruk WSL)
  • Kjør kommando:
openssl pkcs12 -in "path-to-your-sertificate/yourSertificate.p12" -out "some-place-you-know/PemSertificate.pem" -nodes -clcerts
  • Resultatet i PemSertificate.pem skal se omtrent slik ut:
Bag Attributes
    friendlyName: Authentication certificate || Issuer certificate || Root certificate || Organisasjonsnavn TEST
    localKeyID: xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx
Key Attributes: <No Attributes>
-----BEGIN PRIVATE KEY-----
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablublabla
blablablablablablabalabblablablablablablabalabblablablablablabla
-----END PRIVATE KEY-----
Bag Attributes
    friendlyName: ORGANISASJONEN DIN
    localKeyID: xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx 
subject=C = NO, O = ORGANISASJONEN DIN, OU = XX, CN = ORGANISASJONEN DIN, serialNumber = xxxxxxxxx

issuer=C = NO, O = RompePass AS-12345678, CN = RompePass Class 3 blabla4 CA 3

-----BEGIN CERTIFICATE-----
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
-----END CERTIFICATE-----
2.1.2 Hent ut relevant sertifikat og tilhørende nøkkel
  • Mot Maskinporten er du ute etter sertifikat og key av typen friendlyName: Authentication certificate eller friendlyName: <org.navn> TEST
  • Kopier ut Authentication certificate fra og med "-----BEGIN CERTIFICATE-----" til og med "-----END CERTIFICATE-----", og lagre det i en egen fil kalt "cert.pem"
  • Kopier ut Authentication certificate fra og med "-----BEGIN PRIVATE KEY----" til og med "-----END PRIVATE KEY-----", og lagre det i en egen fil kalt "private.key"
  • Du skal nå ha to filer seende omtrent slik ut:

cert.pem

-----BEGIN CERTIFICATE-----
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
-----END CERTIFICATE-----

private.key

-----BEGIN PRIVATE KEY-----
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablablabla
blablablablablablabalabblablablablablablabalabblablablablublabla
blablablablablablabalabblablablablablablabalabblablablablablabla
-----END PRIVATE KEY-----
  • Merk deg hvor du lagrer filene (og lagre de meget sikkert)
2.1.3 Konverter cert.pem og private.key til base64-strings
  • Naviger i en terminal til der du har lagret cert.pem og private.key
  • Kjør kommando node
  • Kjør kommando
const fs=require('fs');console.log(Buffer.from(fs.readFileSync('./cert.pem')).toString('base64'))
  • Lagre outputen på en trygg plass som MASKINPORTEN_CERT=<OUTPUT>
  • Kjør kommando
console.log(Buffer.from(fs.readFileSync('./private.key')).toString('base64'))
  • Lagre outputen på en trygg plass som MASKINPORTEN_PRIVATE_KEY=<OUTPUT>
  • !!! Hvis du nå har lagret alt lokalt bør du slette sertifikatene + "cert.pem" + "private.key". Base64-nøklene er alt du trenger. Sertifikatene skal ikke ligge å slenge rundt.

2.1.4 Sette opp spørring mot maskinporten

  • For å få en token fra maskinporten kan man gjøre følgende:
const options = {
    url: 'url for hente token fra maskinporten', // Sjekk ulike endepunkter her: https://docs.digdir.no/docs/Maskinporten/maskinporten_func_wellknown.html
    pemcert: 'et PEM sertifikat som BASE64',
    pemprivateKey: 'en PEM privat nøkkel som BASE64',
    audience: 'https://maskinporten.no', // // Sjekk ulike audience her: https://docs.digdir.no/docs/Maskinporten/maskinporten_func_wellknown.html
    issuer: 'klientID-guid fra maskinporten klienten du har satt opp',
    scope: 'prefix:scope', // Scopet du vil ha token for
}
try {
    const token = await maskinportenToken(options)
    console.log(token)
} catch (error) {
    console.log('Something went wrong. ', error)
}

2.2 PFX (denne er chillest)

2.2.1 Konvertere P12 til PFX

Rename sertifikatet fra .p12 til .pfx. Tada.

2.1.4 Sette opp spørring mot maskinporten

  • For å få en token fra maskinporten kan man gjøre følgende:
const options = {
    url: 'url for hente token fra maskinporten', // Sjekk ulike endepunkter her: https://docs.digdir.no/docs/Maskinporten/maskinporten_func_wellknown.html
    pfxcert: 'et PFX sertifikat som BASE64',
    privateKeyPassphrase: 'krypertingspassordet for sertifikatets privatekey',
    audience: 'https://maskinporten.no', // // Sjekk ulike audience her: https://docs.digdir.no/docs/Maskinporten/maskinporten_func_wellknown.html
    issuer: 'klientID-guid fra maskinporten klienten du har satt opp',
    scope: 'prefix:scope', // Scopet du vil ha token for
}
try {
    const token = await maskinportenToken(pfxOptions)
    console.log(token)
} catch (error) {
    console.log('Something went wrong. ', error)
}