"use strict";(self.webpackChunkgrafana=self.webpackChunkgrafana||[]).push([[3082],{"./public/app/features/api-keys/ApiKeysPage.tsx":(e,n,s)=>{s.r(n),s.d(n,{ApiKeysPageUnconnected:()=>H,default:()=>W});var t=s("../../opt/drone/yarncache/react-npm-17.0.2-99ba37d931-b254cc17ce.zip/node_modules/react/index.js"),i=s("./.yarn/__virtual__/react-redux-virtual-7ad20a440e/3/opt/drone/yarncache/react-redux-npm-7.2.6-134f5ed64d-0bf142ce0d.zip/node_modules/react-redux/es/index.js"),a=s("./public/app/core/selectors/navModel.ts");const r=e=>e.includeExpired?e.keysIncludingExpired.length:e.keys.length,o=e=>{const n=RegExp(e.searchQuery,"i");return(e.includeExpired?e.keysIncludingExpired:e.keys).filter((e=>n.test(e.name)||n.test(e.role)))},l=e=>e.includeExpired,c=e=>0===e.keys.length&&e.keysIncludingExpired.length>0;var d=s("./public/app/core/services/backend_srv.ts"),p=s("./public/app/features/api-keys/state/reducers.ts");function u(){return async e=>{e((0,p.dF)());const[n,s]=await Promise.all([(0,d.i)().get("/api/auth/keys?includeExpired=false"),(0,d.i)().get("/api/auth/keys?includeExpired=true")]);e((0,p.iK)({keys:n,keysIncludingExpired:s}))}}var h,y,x=s("./public/app/core/components/Page/Page.tsx"),m=s("./.yarn/__virtual__/@emotion-css-virtual-72c314ddb1/3/opt/drone/yarncache/@emotion-css-npm-11.7.1-25ff8755a7-ac1f56656f.zip/node_modules/@emotion/css/dist/emotion-css.esm.js"),g=s("./packages/grafana-ui/src/index.ts"),f=s("../../opt/drone/yarncache/react-npm-17.0.2-99ba37d931-b254cc17ce.zip/node_modules/react/jsx-runtime.js");function b(e){let{onDismiss:n,apiKey:s,rootPath:t}=e;const i=(0,g.useStyles2)(j);return(0,f.jsxs)(g.Modal,{title:"API Key Created",onDismiss:n,onClickBackdrop:n,isOpen:!0,children:[(0,f.jsx)(g.Field,{label:"Key",children:(0,f.jsx)("span",{className:i.label,children:s})}),h||(h=(0,f.jsx)(g.Alert,{severity:"info",title:"You will only be able to view this key here once!",children:"It is not stored in this form, so be sure to copy it now."})),y||(y=(0,f.jsx)("p",{className:"text-muted",children:"You can authenticate a request using the Authorization HTTP header, example:"})),(0,f.jsxs)("pre",{className:i.small,children:['curl -H "Authorization: Bearer ',s,'" ',t,"/api/dashboards/home"]})]})}function j(e){return{label:m.css`
      padding: ${e.spacing(1)};
      background-color: ${e.colors.background.secondary};
      border-radius: ${e.shape.borderRadius()};
    `,small:m.css`
      font-size: ${e.typography.bodySmall.fontSize};
      font-weight: ${e.typography.bodySmall.fontWeight};
    `}}var v,k,A,C=s("./public/app/core/config.ts"),K=s("./public/app/core/app_events.ts"),w=s("./public/app/core/components/EmptyListCTA/EmptyListCTA.tsx"),I=s("./packages/grafana-data/src/index.ts"),E=s("./public/app/features/profile/state/selectors.ts"),S=s("./public/app/types/index.ts"),N=s("./public/app/core/components/Animations/SlideDown.tsx"),P=s("./public/app/core/components/CloseButton/CloseButton.tsx");const{Input:T}=g.LegacyForms,D=Object.keys(S.B5).map((e=>({label:e,value:e})));function _(e){if(!e)return!0;try{return I.rangeUtil.intervalToSeconds(e),!0}catch{}return!1}const F={[g.EventsWithValidation.onBlur]:[{rule:_,errorMessage:"Not a valid duration"}]},Z=e=>{let{show:n,onClose:s,onKeyAdded:i}=e;const[a,r]=(0,t.useState)(""),[o,l]=(0,t.useState)(S.B5.Viewer),[c,d]=(0,t.useState)("");(0,t.useEffect)((()=>{r(""),l(S.B5.Viewer),d("")}),[n]);return(0,f.jsx)(N.s,{in:n,children:(0,f.jsxs)("div",{className:"gf-form-inline cta-form",children:[(0,f.jsx)(P.P,{onClick:s}),(0,f.jsxs)("form",{className:"gf-form-group",onSubmit:e=>{e.preventDefault(),_(c)&&(i({name:a,role:o,secondsToLive:c}),s())},children:[v||(v=(0,f.jsx)("h5",{children:"Add API Key"})),(0,f.jsxs)("div",{className:"gf-form-inline",children:[(0,f.jsxs)("div",{className:"gf-form max-width-21",children:[k||(k=(0,f.jsx)("span",{className:"gf-form-label",children:"Key name"})),(0,f.jsx)(T,{type:"text",className:"gf-form-input",value:a,placeholder:"Name",onChange:e=>{r(e.currentTarget.value)}})]}),(0,f.jsx)("div",{className:"gf-form",children:(0,f.jsx)(g.InlineField,{label:"Role",children:(0,f.jsx)(g.Select,{inputId:"role-select",value:o,onChange:e=>{l(e.value)},options:D,menuShouldPortal:!0})})}),(0,f.jsx)("div",{className:"gf-form max-width-21",children:(0,f.jsx)(g.InlineField,{tooltip:"The API key life duration. For example, 1d if your key is going to last for one day. Supported units are: s,m,h,d,w,M,y",label:"Time to live",children:(0,f.jsx)(T,{id:"time-to-live-input",type:"text",placeholder:"1d",validationEvents:F,value:c,onChange:e=>{d(e.currentTarget.value)}})})}),A||(A=(0,f.jsx)("div",{className:"gf-form",children:(0,f.jsx)(g.Button,{children:"Add"})}))]})]})]})})},B=e=>{let{searchQuery:n,disabled:s,onAddClick:t,onSearchChange:i}=e;return(0,f.jsxs)("div",{className:"page-action-bar",children:[(0,f.jsx)("div",{className:"gf-form gf-form--grow",children:(0,f.jsx)(g.FilterInput,{placeholder:"Search keys",value:n,onChange:i})}),(0,f.jsx)(g.Button,{className:"pull-right",onClick:t,disabled:s,children:"Add API key"})]})};var Q,z,M;const L=e=>{let{apiKeys:n,timeZone:s,onDelete:t}=e;const i=(0,g.useTheme2)(),a=R(i);return(0,f.jsxs)("table",{className:"filter-table",children:[(0,f.jsx)("thead",{children:(0,f.jsxs)("tr",{children:[Q||(Q=(0,f.jsx)("th",{children:"Name"})),z||(z=(0,f.jsx)("th",{children:"Role"})),M||(M=(0,f.jsx)("th",{children:"Expires"})),(0,f.jsx)("th",{style:{width:"34px"}})]})}),n.length>0?(0,f.jsx)("tbody",{children:n.map((e=>{const n=Boolean(e.expiration&&Date.now()>new Date(e.expiration).getTime());return(0,f.jsxs)("tr",{className:a.tableRow(n),children:[(0,f.jsx)("td",{children:e.name}),(0,f.jsx)("td",{children:e.role}),(0,f.jsxs)("td",{children:[$(e.expiration,s),n&&(0,f.jsx)("span",{className:a.tooltipContainer,children:(0,f.jsx)(g.Tooltip,{content:"This API key has expired.",children:(0,f.jsx)(g.Icon,{name:"exclamation-triangle"})})})]}),(0,f.jsx)("td",{children:(0,f.jsx)(g.DeleteButton,{"aria-label":"Delete API key",size:"sm",onConfirm:()=>t(e)})})]},e.id)}))}):null]})};function $(e,n){return e?(0,I.dateTimeFormat)(e,{timeZone:n}):"No expiration date"}const R=e=>({tableRow:n=>m.css`
    color: ${n?e.colors.text.secondary:e.colors.text.primary};
  `,tooltipContainer:m.css`
    margin-left: ${e.spacing(1)};
  `}),O=e=>{let{children:n}=e;const[s,i]=(0,t.useState)(!1),a=(0,t.useCallback)((()=>{i(!s)}),[s]);return n({isAdding:s,toggleIsAdding:a})};var U=s("./public/app/types/events.ts");function V(e,n,s){return n in e?Object.defineProperty(e,n,{value:s,enumerable:!0,configurable:!0,writable:!0}):e[n]=s,e}const q={loadApiKeys:u,deleteApiKey:function(e){return async n=>{(0,d.i)().delete(`/api/auth/keys/${e}`).then((()=>n(u())))}},setSearchQuery:p.ql,toggleIncludeExpired:function(){return e=>{e((0,p.j4)())}},addApiKey:function(e,n){return async s=>{const t=await(0,d.i)().post("/api/auth/keys",e);s((0,p.ql)("")),s(u()),n(t.key)}}},Y=(0,i.connect)((function(e){return{navModel:(0,a.h)(e.navIndex,"apikeys"),apiKeys:o(e.apiKeys),searchQuery:e.apiKeys.searchQuery,apiKeysCount:r(e.apiKeys),hasFetched:e.apiKeys.hasFetched,timeZone:(0,E.Z)(e.user),includeExpired:l(e.apiKeys),includeExpiredDisabled:c(e.apiKeys)}}),q);class H extends t.PureComponent{constructor(e){super(e),V(this,"onDeleteApiKey",(e=>{this.props.deleteApiKey(e.id)})),V(this,"onSearchQueryChange",(e=>{this.props.setSearchQuery(e)})),V(this,"onIncludeExpiredChange",(e=>{this.props.toggleIncludeExpired()})),V(this,"onAddApiKey",(e=>{const n=e=>{const n=window.location.origin+C.ZP.appSubUrl;K.Z.publish(new U.Dn({props:{apiKey:e,rootPath:n},component:b}))},s=e.secondsToLive;try{const t=s?I.rangeUtil.intervalToSeconds(s):null,i=Object.assign({},e,{secondsToLive:t});this.props.addApiKey(i,n),this.setState((e=>Object.assign({},e,{isAdding:!1})))}catch(e){console.error(e)}}))}componentDidMount(){this.fetchApiKeys()}async fetchApiKeys(){await this.props.loadApiKeys()}render(){const{hasFetched:e,navModel:n,apiKeysCount:s,apiKeys:t,searchQuery:i,timeZone:a,includeExpired:r,includeExpiredDisabled:o}=this.props;return e?(0,f.jsx)(x.Z,{navModel:n,children:(0,f.jsx)(x.Z.Contents,{isLoading:!1,children:(0,f.jsx)(O,{children:e=>{let{isAdding:n,toggleIsAdding:l}=e;const c=!n&&0===s,d=s>0;return(0,f.jsxs)(f.Fragment,{children:[c?(0,f.jsx)(w.Z,{title:"You haven't added any API keys yet.",buttonIcon:"key-skeleton-alt",onClick:l,buttonTitle:"New API key",proTip:"Remember, you can provide view-only API access to other applications."}):null,d?(0,f.jsx)(B,{searchQuery:i,disabled:n,onAddClick:l,onSearchChange:this.onSearchQueryChange}):null,(0,f.jsx)(Z,{show:n,onClose:l,onKeyAdded:this.onAddApiKey}),d?(0,f.jsxs)(g.VerticalGroup,{children:[(0,f.jsx)(g.InlineField,{disabled:o,label:"Include expired keys",children:(0,f.jsx)(g.InlineSwitch,{id:"showExpired",value:r,onChange:this.onIncludeExpiredChange})}),(0,f.jsx)(L,{apiKeys:t,timeZone:a,onDelete:this.onDeleteApiKey})]}):null]})}})})}):(0,f.jsx)(x.Z,{navModel:n,children:(0,f.jsx)(x.Z.Contents,{isLoading:!0})})}}const W=Y(H)}}]);
//# sourceMappingURL=ApiKeysPage.cd4ada1146b53ad3284f.js.map